13

I'm reading the contents of file into a 9 element array. I need to check if there are any duplicates in this array. I need to do this without reordering or changing the any of the contents of the array.

How would I go about doing this?

1

3 Answers 3

24

Use brute force.

You've only got 9 elements in the array, so it'll only take 36 comparisons to find any duplicates:

int count = sizeof(array) / sizeof(array[0]);

for (int i = 0; i < count - 1; i++) { // read comment by @nbro
    for (int j = i + 1; j < count; j++) {
        if (array[i] == array[j]) {
            // do whatever you do in case of a duplicate
        }
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

By sizeof(array) you of course mean sizeof(array) / sizeof(array[0])?
The first and outer loop only needs to go up to count - 1, because otherwise the inner loop at the last iteration is completely useless. I will edit the answer to reflect this improvement.
I am wondering and almost unable to figure out how did you count the comparison numbers @Caleb. If you have time, could you please elaborate a little bit about it? I am interested to know and forgot the actual way to do so. Thanks.
@AT-2017 Say you've got 9 items in an array and want to compare each pair of items: start with the first one, and compare it to each of the other 8 items in the array. That's 8 comparisons so far. Then move to the second one. No need to compare it to the first item, you already did that, so compare to each of the remaining 7 items. Repeat this process and you'll end up with 8+7+6+5+4+3+2+1=36 comparisons. And that's just what the code above does.
@jangorecki I could, but then I'd be answering a different question than the one the OP asked, and we have lots of other C-language questions about finding unique elements, removing duplicates, etc. In fact, if you search for questions tagged with both c and duplicates, you'll find (as I'm writing this) 113 questions. The best non-brute-force solution will depend on the question, e.g.: Do you just need to detect dupes, or remove them in place, or create a new array of unique values? If you have a specific question, ask it.
|
0

You can use this method:

// sort a copy of the array with the algorithm you like the most and then...
bool duplicates = false;
for(i = 0; i < 7; i++)
{
   if (array[i] == array[i+1])
   {
      duplicates = true;
      break;
   }
}

4 Comments

The OP has indicated that array modifications should be avoided. Also, $ is not a valid character in C's identifiers per the standard, although your compiler may happen to support it. Oh, and you're accessing a non-existing array element, array[9] when i=8.
@Alex Where did you see the $?
The Q says reordering the array isn't acceptable. You could copy the array first, sort the copy, and then look for duplicates using your code above, but that wouldn't tell you which indices in the original array are the duplicates.
@Caleb The OP doesn't ask for the index. It actually just want to know if there's a duplicate.
-6
 int main()
 {
 srand(time(0));
 int data[10];

for(int c=0;c<=9;c++)
{
    bool found = false;
    while(!found)
    {
        data[c]=rand()%10+1;
        found=true;
        for(int r=0;r<c;r++)
        {
            if(data[c]==data[r]) found=false;
        }
    }
}

for(int c=0;c<=9;c++) cout << data[c] << endl;
return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.