Yesterday, a friend suddenly asked me this question, for a moment I was speechless, just at the beginning is limited to no way to record the repeat position, as just contact with the Java language rookie, racked his brains to the most stupid way to achieve, but also hope the great God pointing out a better way.
public class Test01 {
public static void Main (string[] args) {
Test1 ();
}
/**
* Method Name: Test1
* Method Description: An array to remove duplicate numbers, not with the help of Java tools class, the method is more clumsy, but also ask the master pointing
* @param: Test01
* @return: void
* @Author: it Oriole
* @create Date:dec 9, 2014
*/
private static void Test1 () {
Int[] Arry1 = {1,2,1,3,1,23,14,12,321,2,5,5,1,32,2,13,12};
Statistics of repeated numbers
boolean[] repeat = new boolean[arry1.length];
for (int i = 0;i<arry1.length;i++) {
for (int j = 0;j<arry1.length;j++) {
if (I>j&&arry1[i]==arry1[j]) {//If the number is compared to each of his subsequent numbers if equal repeat[j] position is set to True
Repeat[j]=true;
}
}
}
Calculates the length of the second array
int lenth = 0;
for (int i = 0;i<repeat.length;i++) {
if (repeat[i]==false) lenth++;
}
int[] Arry2 = new int[lenth];//to define an array to receive non-repeating numbers
int temp = 0;//records the length of the second array
Transfer all the numbers to the new array
for (int i = 0;i<arry1.length;i++) {
if (Repeat[i]==false) {
Arry2[temp]=arry1[i];
temp++;
}
}
Print results Validation
for (int s:arry2) {
System.out.print (s+ ",");
}
SYSTEM.OUT.PRINTLN ("Total number of repetitions" + (repeat.length-lenth));
}
}
Apply Java to remove duplicate numbers from a one-dimensional array without using any Java tool classes.