Removing duplicate elements in array

...
Last edited on
Looks like you never gave your find function a body:

1
2
3
int find(const char arr[], int size, char target, int position)
{
}
Copied the wrong thing. It's up now.
The problem I'm getting, which I assume is why it won't get rid of if, is size = size - count is for some reason returning
Error 2 error C2466: cannot allocate an array of constant size 0 c:\users\steven\documents\visual studio 2013\projects\project1\project1\source.cpp 48 1 Project1
and
Error 3 error C2133: 'NewArr' : unknown size c:\users\steven\documents\visual studio 2013\projects\project1\project1\source.cpp 48 1 Project1

Currently trying to track down the probelm
Last edited on
Didn't really test it or anything but a few things that jumped out at me:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void removeDuplicates(char arr[], int& size)
{

     int i = 0;
    for(int j = 0; j < size; j++)
        {
            int result = find(arr, size, arr[j], i);
            if(arr[j] != result)
                j++;
            else
            {
                erase(arr, size, i, 1);
                j++;
            }
        }


}


On line 7 you are passing in i for the position, but i will always be 0 since you never increase it, I think you are meaning to increase it on lines 9 and 13 instead of increasing j, which is already being incremented by the for loop.

Also on line 8, you are checking (arr[j] and result), result is the position of the matching element (returned by find) and arr[j] is the actual element you checked. I think you meant to do if (j != result)? Not sure since like I said I didn't test it
Topic archived. No new replies allowed.