This topic is definitely not operable in the original array, then we think of using another array to assist.
So here's the problem. It is good to know how long the new array is. Same as the original array. But if there are 0 in the original array, then there is no way to judge.
Because the array is not worth talking to, the default is 0. But if we know the number of repetitions, then the length of the new array is very good.
public class arraytest{
/**
* @author Prodigal Son--not back
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
Int[] arr={1,2,1,1,1};
int len=arr.length;
System.out.println (len);
int count=0;//Statistics The number of repeating elements in an array
int count2=0;
System.out.print ("The array before processing is:");
for (int x:arr) {
System.out.print (x+ "");
}
System.out.println (len);
for (int i = 0; i < arr.length-1; i++) {
for (int j = i+1 J < Arr.length; J + +) {
if (Arr[i]==arr[j]) {
count++;//If there is equal, add 1, then break
break;//prevent back repeat Plus
}
}
}
int len1=len-count;
System.out.println (LEN1);
int k=0;//The new array's Corner mark
Int[] Newarr=new Int[len1];
for (int i = 0; i < arr.length-1;) {
for (int j = I+1;j < Arr.length; J + +) {
if (Arr[i]!=arr[j]) {
Number of times count2++;//statistics are not equal
}
}
If the number of times that is not equal equals the number of comparisons, put that number in the new array
if (count2== (arr.length-1-i)) {
Newarr[k]=arr[i];
System.out.println (Newarr[k]);
k++;
count2=0;
}
else{
Otherwise, put the counter that doesn't count equal to 0
count2=0;
}
i++;
If it's the last one in the last array, it's directly assigned to the new array.
if (i==arr.length-1) {
Newarr[k++]=arr[i];
}
}
System.out.println ();
System.out.print ("The Processed array is:");
for (int x:newarr) {
System.out.print (x+ "");
}
}
}