Remove duplicate elements in an array in Java
Description
The following code shows how to remove duplicate elements in an array.
Example
/*from w w w .ja va 2s . c o m*/
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] argv) throws Exception {
int[] array = new int[]{1,2,2,2,2,2,2,2,2,2,2,2,2,2};
Set set = new HashSet(Arrays.asList(array));
System.out.println(set);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »