Repeated numbers in the array

Source: Internet
Author: User

The title describes all the numbers in an array of length n in the range of 0 to n-1. Some of the numbers in the array are duplicates, but it is not known that several numbers are duplicates. I don't know how many times each number repeats. Please find any duplicate numbers in the array. For example, if you enter an array of length 7 {2,3,1,0,2,5,3}, then the corresponding output is a repeating number of 2 or 3. Normal thinking: With a bit type of array, the length of an array of N, if traversed to 3, then the arr[3] set to 1, the next time to find 3, see arr[3] 1. Description 3 appeared, did not appear on the arr[2] set to 1 This time the complexity of O (N)     The spatial complexity is O (N). It is possible to use an array of its own, without requesting a secondary space, so that the time complexity is n and the space complexity is 1. Idea: Use the index of each number as an array to find the position, and add N to the number in that position. In the past, when looking for the index in the process of traversal, when encountering a number larger than n, we subtract n, as an index (the number in the array is the same, just use the number in the array minus n as the index to find the position in the array), when the number found by the index is greater than N, That means that the same index has been found before him, and because the index is looked up from the array, then there must be two in the array is the same, that is the index. Time complexity O (N), Spatial complexity O (1) procedure
 Public classSolution {//Parameters://numbers:an array of integers//length:the length of array numbers//duplication: (Output) The duplicated number in the array number,length of duplication array is 1,so using Duplicat Ion[0] =?    in implementation; //Here duplication-like Pointor-C + +, duplication[0] equal *duplication in C/C + +//here to pay special attention ~ return any duplicate one, assignment value duplication[0]//Return Value:true If the input is valid, and there be some duplications in the array number//otherwise false     Public BooleanDuplicateintNumbers[],intLengthint[] duplication) {         for(inti=0;i<length;i++){            intindex=Numbers[i]; if(index>=length) {Index-=length; }            if(numbers[index]>length) {duplication[0]=index; return true; }Else{Numbers[index]+=length; }} duplication[0]=-1; return false; }}    

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.