2-repeated numbers in the array

Source: Internet
Author: User

Tag: temp equals length enter its amp start count description

Problem Description: Find the duplicate numbers in the array
All the numbers in an array of length n are in the range of 0~n-1, and some of the numbers in the array are duplicates, but I don't know how many numbers are duplicated, and I don't know how many repetitions. Please find any duplicate numbers in the array.

    • Sort
    • Hash table. Scan each number of arrays sequentially from beginning to end, each scan to a number, can use O (1) time to determine whether the hash table already contains the number. If the hash table does not already have this number, add it to the hash table, and if that number exists in the hash table, a duplicate number is found. The time complexity of this algorithm is O (n), but it increases the time efficiency at the cost of a hash table of size O (n).
    • Rearrange the array. Array elements are between 0~n-1, and if the array does not have duplicate numbers, then when the array is sorted, the number I will appear in the position labeled I. Scan every number in this array from beginning to end, and when scanning to a number labeled I, first compare the number (m) is not equal to I; if it is, then scan the next number, if not, then compare it with the first m number, if it is equal to the number m, it will find a duplicate number, If it is not equal to the number m, the first and second numbers are exchanged, and the M is placed in its place. Next, repeat the comparison and exchange process until a duplicate number is found.
bool duplicate(int numbers[], int length, int* duplication){    if(numbers == nullptr || length < 0)        return false;    for(int i=0; i < length; i++){        if(numbers[i]<0 || numbers[i]>length-1)            return false;    }    for(int i=0; i < length; i++){        while(numbers[i] != i){            if(numbers[i] == numbers[numbers[i]]){                *duplication = numbers[i];                return true;            }            int temp = numbers[i];            numbers[i] = numbers[temp];            numbers[temp] = temp;        }    }    return false;}

Second, problem description: Do not modify the array to find duplicate numbers
All numbers in an array of length n+1 are within the range of 1-n, so at least one number in the array is duplicated. Please find any duplicate numbers in the array, but cannot modify the array

    • creates a secondary array of length n+1, and then copies each number of the original array to the secondary array one after the other. If the number copied in the original array is m, copy it to the position labeled M in the secondary array, so it is easy to see which number is duplicated. The scheme requires the number of digits in a range of auxiliary space
    • for O (n). Divide the 1-n number from the middle of the number m into two parts, the front half is 1-m, the back half is m+1-n. If the number of 1-m is more than M, then the half of the interval must contain a repeating number, otherwise the interval of the other half of the m+1-n must contain duplicate numbers. Continue dividing the interval containing the repeating number until a duplicate number is found. Similar to the binary lookup algorithm, it is just a step more than the number of numbers in the statistical interval.
int countRange(const int* numbers, int length, int start, int end){    if(numbers == nullptr)        return 0;    int count = 0;    for(int i=0; i < length; i++){        if(numbers[i]>=start && numbers[i]<=end){            ++count;        }    }    return count;}int getDuplication(const int* numbers, int length){    if(numbers==nullptr || length<=0)        return -1;    int start = 1;    int end = length-1;    while(end >= start){        int middle = (end-start)>>2 + 1;        int count = countRange(numbers, length, start, middle);        if(end == start){            if(count > 1)                return start;            else                break;        }        if(count > (middle-start+1))            end = middle;        else            start = middle+1;    }    return -1;}按照二分查找的思路,如果输入长度为n的数组,那么函数countRange()将被调用O(logn)次,每次需要O(n)的时间,因此总的时间复杂度是O(nlogn),空间复杂度为O(1).

2-repeated numbers in the array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.