Monday, October 11, 2010

Remove duplicates from an Array

String[] array = {"A", "X", "G", "X", "A"};

Set set = new HashSet(Arrays.asList(array));
String[] array2 = (String[])(set.toArray(new String[set.size()]));

A set is a collection object that cannot have a duplicate values, hence converting the array to a set removes the duplicate values.

No comments: