-1

The challenge consist in writing a program that deletes the duplicated values of an array.

#include <iostream>
using namespace std;

void func(int arrayA[], int n, int arrayB[]) {
    int h = 0;
    int count = 0;
    arrayB[0] = arrayA[0];
    for (int i = 1; i < n; i++) {
        count = 0;
        for (int j = 0; j < n; j++) {
            if (arrayB[i] == arrayA[j]) {
                count++;
            }
        }
        if (count == 0) {
            h++;
            arrayB[h] = arrayA[i];
        }
    }
}
int main() {
    int n;
    cin >> n;
    int arrayA[n];
    int arrayB[n];
    for (int i = 0; i < n; i++) {
        cin >> arrayA[i];
    }
    func(arrayA, n, arrayB);
    for (int i = 0; i < n; i++) {
        cout << arrayB[i] << endl;
    }
}

My logic was creating a new array and filling it with the non repeated values, for that i created a loop that adds 1 to a counter when it finds a repeated value and if the counter is equal to 0 the value is added. then the counter restarts.

Thank you for your help to a new programmer i really tried to solve it in my own.

David G
  • 94,763
  • 41
  • 167
  • 253
Martin
  • 39
  • 1
  • 1
    `int arrayA[n];` is not valid C++ - array dimensions must be compile-time constants. –  Oct 24 '18 at 19:54
  • Hint: Make a simple input that exposes the misbehaviour. Then work through your code and build a table of expectations. What do you expect to happen at each line of code? If building the table doesn't shake the bug loose, step through the program line by line with a debugger and watch for where the program does something you didn't expect. Odds are good that's a bug and if it isn't it's an error in your expectations. Both need to be fixed before proceeding. – user4581301 Oct 24 '18 at 19:58
  • Note: If you are going to eliminate duplication, odds are high that `arrayB` will have fewer valid contents than `arrayA`. – user4581301 Oct 24 '18 at 20:01
  • 1
    Does the order matter? If not, subvert the problem and store the data in a set and then output it. – NathanOliver Oct 24 '18 at 20:02
  • https://stackoverflow.com/questions/20598235/convert-array-to-set-in-c https://stackoverflow.com/questions/1041620/whats-the-most-efficient-way-to-erase-duplicates-and-sort-a-vector –  Oct 24 '18 at 21:44

1 Answers1

1

Using std::vector:

If you wanted to be lazy about it, try checkout out this example. You could do something like the following (not tested):

std::vector<int> func(int a[], int size_a){
    std::vector<int> vec_a(a, a + size_a); 
    std::sort(vec_a.begin(), vec_a.end()); 
    std::vector<int> ordered_vec_a = std::unique(vec_a.begin(), vec_a.end()); 
    vec_a.erase(ordered_vec_a, vec_a.end()); 
    return ordered_vec_a;
}

"Hardcore" C++:

If you want to attack your problem, and continue your solution, using a much more "raw" approach, you are going to need to make sure that your returned array is exactly the number of unique elements in array a.

So, first, get the number of unique elements of a take a look at this answer. After that, you can create you output array, b:

int b = new int[size_calc_from_post]
//do stuff here.
delete[] b; // make sure to delete! 
b = NULL; 
9301293
  • 514
  • 3
  • 16