/** * @author lautturi.com * Java example: how to remove duplicate elements in java array */ import java.util.*; public class Lautturi { public static void main(String[] args) { int[] array = { 11,4,12,7,55,8,12,8,13,55,12,7 }; Set<Integer> set = new HashSet<>(); for(int i=0;i<array.length;i++) { set.add(array[i]); } Integer[] newArray = set.toArray(new Integer[set.size()]); System.out.println(Arrays.toString(newArray)); for (Integer element : newArray) { System.out.print(element+" "); } } }
output:
[4, 7, 55, 8, 11, 12, 13] 4 7 55 8 11 12 13