Defines a function that clears the repeating elements of the array and returns an array that cannot be wasted.
The code is as follows:
ImportJava.util.*; Public classDemo1 { Public Static voidMain (string[] args) {int[] arr = {11, 2, 4, 2, 10, 11}; Arr=clearrepeat (arr); System.out.println ("Clear the array of repeating elements:" +arrays.tostring (arr)); } Public Static int[] Clearrepeat (int[] arr) { intCount = 0;//record number of repeating elements for(inti = 0; i < arr.length-1; i++) { for(intj = i + 1; J < Arr.length; J + +) { if(Arr[i] = =Arr[j]) {Count++; Break; } } } intNewlength = Arr.length-count; int[] NewArr =New int[Newlength]; intindex = 0; for(inti = 0; i < arr.length; i++) { inttemp =Arr[i]; BooleanFlag =false; for(intj = 0; J < Newarr.length; J + +) { if(temp = =Newarr[j]) {Flag=true; Break; } } if(Flag = =false) {Newarr[index++] =temp; } } returnNEWARR; }}
ImportJava.util.*; Public classDemo1 { Public Static voidMain (string[] args) {int[] arr = {11, 2, 4, 2, 10, 11}; Arr=clearrepeat (arr); System.out.println ("Clear the array of repeating elements:" +arrays.tostring (arr)); } Public Static int[] Clearrepeat (int[] arr) { intCount = 0;//record number of repeating elements for(inti = 0; i < arr.length-1; i++) { for(intj = i + 1; J < Arr.length; J + +) { if(Arr[i] = =Arr[j]) {Count++; Break; } } } intNewlength = Arr.length-count; int[] NewArr =New int[Newlength]; intindex = 0; for(inti = 0; i < arr.length; i++) { inttemp =Arr[i]; BooleanFlag =false; for(intj = 0; J < Newarr.length; J + +) { if(temp = =Newarr[j]) {Flag=true; Break; } } if(Flag = =false) {Newarr[index++] =temp; } } returnNEWARR; }}
Java clears the same elements of an array