Find the unique repeated element in the array
[Problem]
1-1001 is placed in an array containing elements. Only one unique element value is repeated, and only one other element is displayed. Each array element can only be accessed once. design an algorithm to find it. Can you design an algorithm to implement it without supporting the storage space?
[Code]
# Include
# Include
# Include
/* According to the calculation method of the exclusive or method, after each two different numbers perform the exclusive or operation, the result is 1. After each two identical numbers are exclusive or, the result is 0, any number is different from zero or the result is still itself. Therefore, the number of N in array a [N] is different or the result is different from the result of 1 to N-1 or, and the obtained value is the expected value. */Int findRepeat1 (int * a, int len) {int I, res; res = a [0]; for (I = 1; I <len; I ++) {res ^ = I; res ^ = a [I];} return res;}/* uses the special relationship between the subscript and the content stored in the unit to traverse the access unit, once the accessed unit is assigned a tag, the tag is used as the key to discovering duplicate numbers. */Int findRepeat2 (int * a, int len) {int index; index = a [len-1]-1; while (1) {if (a [index] <0) break; a [index] * =-1; index = a [index] * (-1)-1 ;} return index + 1;} int main (void) {int a [] = {1, 2, 3, 4, 5, 6, 4}; int res; res = findRepeat2 (a, 7); printf ("% d \ n", res); return 0 ;}