In Java 8 Stream, filter with Set.Add() is the fastest algorithm to find duplicate elements, because it loops only one time.

  Set<T> items = new HashSet<>();
        .filter(n -> !items.add(n))

The Collections.frequency is the slowest because it compares each item with a list – Collections.frequency(list, i). If we increase the size of the list, the performance will get slower.

return -> Collections.frequency(list, i) > 1)


  1. Pingback: JAVA : example of Arrays.sort() » JavaTuto

Laisser un commentaire