Java tutorial
//package com.java2s; // Licensed under the Apache License, Version 2.0 (the "License"); import java.util.HashSet; import java.util.Set; public class Main { /** * Returns the set of all elements in the given collection that appear more than once. * @param input some collection. * @return the set of repeated elements. May return an empty set, but never null. */ public static <T> Set<T> duplicatedElementsOf(Iterable<T> input) { Set<T> duplicates = new HashSet<>(); Set<T> elementSet = new HashSet<>(); for (T el : input) { if (!elementSet.add(el)) { duplicates.add(el); } } return duplicates; } }