public static <T> Set<T> duplicates(List<T> s) {
        Set<T> set = new HashSet<>();
        Set<T> duplicates = new HashSet<>();

        s.forEach(element -> {
            if (!set.add(element)) {
                duplicates.add(element);
            }
        });
        return duplicates;
    }
Return all duplicates in a list. A duplicated element will appear only once.
tools/src/main/java/org/apache/kafka/tools/ToolsUtils.java