Problem Description: Implement an algorithm that returns an array that removes duplicate elements from the original array and preserves the order of the elements.
For example: input: 2,1,2,3,-9,5,-9,10,
Output: 2,1,3,-9,5,10,
The Java code is as follows:
1 ImportJava.util.*;2 classChuchong {3 Public Static int[] RemoveDuplicates (int[] Array) {4Map<integer, boolean> map =NewHashmap<integer, boolean> ();//used to store the value of an array element that is not duplicated, where key is an element value, value is a flag bit, true indicates existence, and false means that there is no5list<integer> values =NewArraylist<integer> ();//store non-repeating element values6 7 for(intValue:array) {8 if(Map.containskey (value))//determines whether each array value is in the collection map, and if so, skips the value or adds it to the collection map9 Continue;Ten OneMap.put (Value,true); A Values.add (value); - } - the int[] result =New int[Values.size ()]; - for(inti = 0; I < values.size (); i++) {//copies the elements in values into a fixed-length array -Result[i] =Values.get (i); - } + returnResult//returns a reference to the array being evaluated - } + } A at Public classMain { - Public Static voidMain (string[] args) { - int[] a={2,1,2,3,-9,5,-9,10}; -System.out.print ("Original array is:"); - for(inti=0;i<a.length;i++) -System.out.print (a[i]+ ","); in System.out.println (); -System.out.print ("The array after removing duplicate values is:"); to int[] b=Chuchong.removeduplicates (a); + for(inti=0;i<b.length;i++) -System.out.print (b[i]+ ","); the } * $}
View Code
The output is:
The original array is: 2,1,2,3,-9,5,-9,10,
The array after removing duplicate values is: 2,1,3,-9,5,10,
Array element de-weight