I accept a mobile numbers array, and I need to remove repeated numbers in the array. The way I choose is converting the array to a hashset, then convert it back to an array. Is there any better way?
My code is below:
public static void main (String[] args) {
String[] mobiles = new String[]{"1", "2", "3", "1", "1"};
Set<String> data = new HashSet<>(Arrays.asList(mobiles));
String[] result = data.toArray(new String[0]);
for (String s : result) {
System.out.println(s);
}
}