How can I find duplicates in array when there is more than one duplicated element?
When the array is only one duplicated element (for example: 1, 2, 3, 4, 4, 4, 5, 6, 7) then it is very easy:
int duplicate(int* a, int s)
{
int x = a[0];
for(int i = 1; i < s; ++i)
{
x = x ^ a[i];
}
for(int i = 0; i < a[s]; ++i)
{
x = x ^ i;
}
return x;
}
But if the input array contains more than one duplicated element (for example: 1, 2, 2, 2, 3, 4, 4, 4, 5, 6, 7), the above won't work. How can we solve this problem in O(n) time?
*(a+s-1)instead ofa[s-1])?duplicate()is supposed to return? currently, you're returning this value({XOR:a[i]} XOR 1..a[s-1])which doesn't make sense to me