01-- find an array duplicate numbers

/ ** topic

* All numbers in an array of length n's are in the range of 0 to n-1.

* Certain numbers are repeated in the array, but do not know how many numbers repeat, do not know each number was repeated several times.

* Look for a repeat of the array in any number. For example, if the length of the input array 7 {2, 3, 1, 0, 2, 5, 3}, then the corresponding output is repeatable number 2 or 3.

*/

 

/ ** ideas:

* 1, allowing the user to enter the specified array length, and then prepare an array of data (not considering illegal user input data)

* 2, to re-set by the array, then two like arrays Save, to obtain a duplicate array of numbers contains only; (Note that this array data duplication is still possible, it is necessary to continue to weight)

* 3, the output

*/

 

import java.util.*;
public class Demo {
public static void main ( String[] args ) {

 //------------------------------------------initial preparation work--- -------------------------------------------------- -

// length of the input array;
Scanner Scanner new new SC = (the System.in);
of System.out.print ( "Please enter the range (0 ~ n-):" + "\ T");
int = n-sc.nextInt ();

// array of questions asked
the ArrayList <Integer> = new new ARR the ArrayList <> ();
// deduplication array
HashSet <Integer> = MSET new new HashSet <> ();

the Random new new RAN = the Random ();
// random n times, the number n is generated, stored in the array
for (int I = 0; I <n; I ++) {
// generates a 0 ~ n 1-random number between, into an array
int j = ran. the nextInt (n-);
arr.add (J);
mset.add (J);
}
System.out.println ( "----------------------- - [original data] ------------------------ ");
System.out.println (" random array is: "+arr);
System.out.println ( "weight to the reference value:" + mset);

 // ------------------------------------------ data manipulation ---- -------------------------------------------------- -

// At this time, to obtain an array of in line with requirements of the subject; and no duplicate set a set
IF (arr.size () <= 0 || ARR == null) {
System.out.println ( "Invalid Array!" );
}
IF (arr.size () == mset.size ()) {
System.out.println ( "repeat-free digital array!");
} the else {
// duplicate numbers, repeating array is: arr array subtracts the repeated array MSET
for (Integer Integer: MSET) {
// get the first index number of the array arr again, remove
int arr.indexOf I = (Integer);
arr.remove (I);
}

/ / go weight
HashSet <Integer> = new new HashSet List <> ();
for (Integer I: ARR) {
List.add (I);
}
// repeat number is:
System.out.println ( "duplicate values:" + List);
}

}

}

Guess you like

Origin www.cnblogs.com/eiyuan/p/11250342.html