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)