/** * @author lautturi.com * Java example: find out duplicate elements in java array */ import java.util.*; public class Lautturi { public static void main(String[] args) { int[] numArray = { 11,4,12,7,55,8,12,8,13,55,12,7 }; Set<Integer> set = new HashSet<>(); for (Integer num : numArray) { if (set.add(num) == false) { // print the duplicate element System.out.println("duplicate element found:"+num); } } } }Sourww:ecw.lautturi.com
output:
duplicate element found:12 duplicate element found:8 duplicate element found:55 duplicate element found:12 duplicate element found:7