Find duplicate in array with a memory efficient approach
A is an array of integers. All the values are between 0 to A.Length-1 it means 0 <= A[i] <= A.Length-1 I am supposed to find repeating elements; and if there are several repeating elements, then choose the one that has lower index for the repeated item. for example: a = [3, 4, 2, 5, 2, 3] then result = 2 This was an interview question. I used another array to store items and check when it is repeating. Then it gave me time-out for some test cases. The interviewer advised to only loop over the array only once, and do not create any additional data structure. No need for another data structure. You can use the input itself as a hashset. Every time you see a value, add A.Length to the item that corresponds to that index. As values might have been already incremented, you should look at the value as A[i] mod A.length. If you find an item that is already >= A.length.. you have a repetition. (Remember that the problem states that all items are in the interval [0, A.Length-1]) T...