C program elements of array present in exactly two of three arrays [closed]

Solution 1:

You can use an array of counters to solve this problem. Without writing out the code, the idea is:

  • Declare a 2001 size array A (so, it is indexed [0, 2000]).
  • Initialize all elements of the array to 0.
  • Realize that the i-th index of the array represents a value of i-1000.
  • Use this array to represent how may times a value has been seen.
  • When scanning an input array, take the value in the array, add 1000 to it, and use it as the index to A and increment the array value at that index.
  • After all the input arrays have been scanned, scan the 2001 array for any index j for which A[j] is equal to 2. Then, that means the input value j-1000 has been seen twice.

There is a weakness in this approach, in that if the same value shows up twice in the same array, but not in any other, the program will incorrectly report the value as a match. There are various ways to resolve that, but it involves detecting the duplicates and making sure they do not affect your computation.