line 21 looks wrong. you are comparing addresses. I think you want * instead of &.
and again on 39, you compare addresses.
either I don't get it or you need to review how to use a pointer (or, maybe better, stop using pointers). You can do all this with array index / integers instead of pointers with less weirdness.
the easy way out is to sort the data, then just iterate it one time, all your duplicates will be together thanks to the sorting.
if you don't want to do that, you need to do this:
for everything in the array, check it against *everything else* in the array, but not itself. if you accidentally check it against itself you register a false duplicate and won't ever get -1 for sure.
here is a condensed version without pointers (apart from passing the array, which is a pointer no matter what color you use)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
int ctdup(int *ip, int sizeip)
{
int ret{-1}; //default value. if no dupes found, this will be returned.
int ct; //count most common item
int oldct{};//previous count
for(int i = 0; i < sizeip; i++) //for each one
{
ct = 0; //reset the counter
for(int j = 0; j < sizeip; j++) //check each one against all the others
{
if(j!=i && ip[j]==ip[i]) //if its not the same item, and they are identical
ct++; //count it
}
if(ct && ct > oldct) //if have any duplicates for current item outer loop
{ //and we have more than previous highest count
ret = ip[i]; //save the current item as most popular
oldct = ct; //save its count
}
}
return ret;
}
|
the whole thing would be smaller if you used vectors, if you know those yet. Vectors are smarter arrays. It would be smaller and faster both if you sorted first, which gets rid of a lot of the junk.