Array: an array duplicate numbers

All numbers in a length of n in the array are in the range 0 to n-1. Some digital array is duplicated, but do not know how many numbers are duplicated. Do not know each digit is repeated several times. Please find an array of any one of the duplicate numbers. For example, if the length of the input array 7 {2,3,1,0,2,5,3}, then the corresponding output of the first 2 repeating digits.

 bool duplicate(int numbers[], int length, int* duplication) {
        for(int i = 0; i < length; i++){
            int index = numbers[i]%length;
            if(numbers[index] >= length){
                *duplication = index;
                return 1;
            }
            numbers[index] += length;
        }
        return 0;
    }



This method is wonderful for each number in turn traversed, it can be in the memory array appeared before. Such as {2,2,1,0}, the first cycle index = 2, a [2] = a [2] + 4 = 5, so that, a [2] = 5> 4 array length, it means that 2 numbers appear before. Wonderful ah! ! ! !

 

Deformation: to find any duplicate numbers:

Guess you like

Origin www.cnblogs.com/icehole/p/12129815.html