Repeated numbers in the array

Source: Internet
Author: User

All numbers in an array of length n are within 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.

ImportJava.util.*; 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) {                if(Numbers = =NULL|| Length <= 0)            return false; Set<Integer> set =NewHashset<integer>();  for(inti=0; i<length; i++) {            if(!Set.add (Numbers[i])) {duplication[0] =Numbers[i]; return true; }        }        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.