The number of repetitions in an array of offer (26)

Source: Internet
Author: User
Tags return tag

Title Descriptionall 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. idea One:with bubbling thought, when encountering an equal element, place it in duplication[0], and change the tag to True
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >public class Solution {//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 duplication[0] =?    in implementation; Here duplication-like pointor in C/C + +, duplication[0] equal *duplication in C + +//here to pay special attention ~ return Arbitrarily repeated one, assignment duplication[0]//Return value:true If the input is valid, and there be some duplications in the ARRA Y number//Otherwise false public boolean duplicate (int numbers[],int length,int [] Duplication        {if (length<=1) {return false;        } Boolean Tag=false;                    for (int i=0;i<length-1;i++) {for (int j=i+1;j<length;j++) {if (Numbers[i]==numbers[j]) {                Duplication[0]=numbers[i];    Tag=true;    }}} return tag; }}</span>

idea two: Create another array temp with the same length as numbers, the number in the numbers array as the subscript for temp (temp[numbers[i]), and then do the self-add, when the same number is present such as: 3, then temp[3] The value must be greater than 1
<span style= "FONT-SIZE:18PX;" >public class Solution {public    boolean duplicate (int numbers[],int length,int [] duplication) {                Boolean tag=f Alse;        int []temp=new int[length];        if (length<=1| | Numbers==null) {            return tag;        }        for (int i=0;i<length;i++) {            temp[numbers[i]]++;        }        for (int j=0;j<length;j++) {            if (temp[j]>1) {                duplication[0]=j;                tag=true;            }        }        return tag;    }} </span>


The number of repetitions in an array of offer (26)

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.