You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于删除数组重复元素后重新排序的C++程序技术问询

Fixing Your Duplicate Removal & Sorting Array Program

Hey Thomas, let's get your array program sorted out (pun intended!). You're working on removing duplicates and reordering an array, with the test input sequence 425500—here's how to polish your code and make it work as intended.

First, Fix the Non-Standard Array Issue

Your code uses a variable-length array (int D[ArrayLength];), which isn't supported in standard C++. Instead, use std::vector<int> for a dynamic, safe array that works across all compilers.

Complete & Corrected Code

Here's a full working version that handles input, removes duplicates, sorts the array, and outputs the result—using your test input 425500 as an example:

#include <iostream>
#include <vector>
#include <algorithm> // Needed for sort() and unique()
#include <limits>     // For input validation

using namespace std;

int main() {
    int arrayLength;

    // Prompt for array length with basic input validation
    cout << "How many numbers do you want the array to be? ";
    while (!(cin >> arrayLength) || arrayLength <= 0) {
        cout << "Please enter a positive integer: ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    // Initialize dynamic array (vector)
    vector<int> nums(arrayLength);

    // Input array elements
    cout << "Enter " << arrayLength << " numbers (e.g., 4 2 5 5 0 0):\n";
    for (int i = 0; i < arrayLength; ++i) {
        cout << "Element " << i + 1 << ": ";
        while (!(cin >> nums[i])) {
            cout << "Please enter a valid integer: ";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
    }

    // Step 1: Sort the array (required for unique() to work)
    sort(nums.begin(), nums.end());

    // Step 2: Remove duplicate elements
    auto lastUnique = unique(nums.begin(), nums.end());
    nums.erase(lastUnique, nums.end());

    // Output the result
    cout << "\nArray after removing duplicates and sorting:\n";
    for (int num : nums) {
        cout << num << " ";
    }
    cout << endl;

    return 0;
}

Key Explanations for Your Use Case

  • Sort First: sort() rearranges elements so duplicates are adjacent—this lets unique() easily detect and mark duplicates for removal.
  • Unique & Erase: unique() doesn't actually delete elements; it shifts unique elements to the front and returns an iterator to the first duplicate. erase() then removes everything from that iterator to the end.
  • Input Validation: Added checks to make sure users enter valid positive integers, preventing crashes from bad input.
  • Test Input 425500: If you input 6 elements as 4, 2, 5, 5, 0, 0, the output will be 0 2 4 5—exactly the sorted, duplicate-free result you want.

Quick Tips for Clean Code

  • Avoid using namespace std; in large projects (it can cause name conflicts), but it's fine for small programs like this.
  • Use descriptive variable names (e.g., nums instead of D, arrayLength instead of ArrayLength) to make your code easier to read.

内容的提问来源于stack exchange,提问作者Thomas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:33:59