
package test;
import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set;
public class Over { static int k[] = new int[5];
public static void main(String... ab) {
int aa[] = new int[] { 2, 1, 2, 3 };
int b[] = Over.removeDuplicates(aa);
for (int ac = 0; ac < b.length; ac++) {
System.out.println(b[ac]);
}
}
public static int[] removeDuplicates(int[] a) {
/*
Please implement this method to
remove all duplicates from the original array. Retain the order of the elements and
always retain the first occurrence of the duplicate elements.
For example, parameter: {2,1,2,3}, result: {2,1,3}
*/
List list = Arrays.asList(a);
System.out.println(list);
Set<Integer> set = new HashSet<Integer>(list);
Object obj[] = set.toArray();
Integer o[] = new Integer[obj.length];
for (int i = 0; i < obj.length; i++) {
o[i] = (Integer) obj[i];
}
for (int i = 0; i < o.length; i++) {
k[i] = o[i].intValue();
}
return k;
}
}