diff --git a/core-java-modules/core-java-collections-list-5/src/main/java/com/baeldung/listduplicate/ListDuplicate.java b/core-java-modules/core-java-collections-list-5/src/main/java/com/baeldung/listduplicate/ListDuplicate.java new file mode 100644 index 0000000000..5e3e845916 --- /dev/null +++ b/core-java-modules/core-java-collections-list-5/src/main/java/com/baeldung/listduplicate/ListDuplicate.java @@ -0,0 +1,69 @@ +package com.baeldung.listduplicate; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; +import java.util.stream.Collectors; + +public class ListDuplicate { + public List listDuplicateUsingSet(List list) { + List duplicates = new ArrayList<>(); + Set set = new HashSet<>(); + for (Integer i : list) { + if (set.contains(i)) { + duplicates.add(i); + } else { + set.add(i); + } + } + return duplicates; + } + + public List listDuplicateUsingMap(List list) { + List duplicates = new ArrayList<>(); + Map frequencyMap = new HashMap<>(); + for (Integer number : list) { + frequencyMap.put(number, frequencyMap.getOrDefault(number, 0) + 1); + } + for (int number : frequencyMap.keySet()) { + if (frequencyMap.get(number) != 1) { + duplicates.add(number); + } + } + return duplicates; + } + + public List listDuplicateUsingFilterAndSetAdd(List list) { + Set elements = new HashSet(); + return list.stream() + .filter(n -> !elements.add(n)) + .collect(Collectors.toList()); + } + + public List listDuplicateUsingCollectionsFrequency(List list) { + List duplicates = new ArrayList<>(); + Set set = list.stream() + .filter(i -> Collections.frequency(list, i) > 1) + .collect(Collectors.toSet()); + duplicates.addAll(set); + return duplicates; + } + + public List listDuplicateUsingMapAndCollectorsGroupingBy(List list) { + List duplicates = new ArrayList<>(); + Set set = list.stream() + .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())) + .entrySet() + .stream() + .filter(m -> m.getValue() > 1) + .map(Map.Entry::getKey) + .collect(Collectors.toSet()); + duplicates.addAll(set); + return duplicates; + } +} diff --git a/core-java-modules/core-java-collections-list-5/src/test/java/com/baeldung/listduplicate/ListDuplicateUnitTest.java b/core-java-modules/core-java-collections-list-5/src/test/java/com/baeldung/listduplicate/ListDuplicateUnitTest.java new file mode 100644 index 0000000000..0d81387aea --- /dev/null +++ b/core-java-modules/core-java-collections-list-5/src/test/java/com/baeldung/listduplicate/ListDuplicateUnitTest.java @@ -0,0 +1,68 @@ +package com.baeldung.listduplicate; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ListDuplicateUnitTest { + private static ListDuplicate listDuplicate; + + @BeforeClass + public static void setup() { + listDuplicate = new ListDuplicate(); + } + + @Test + public void givenList_whenUsingSet_thenReturnDuplicateElements() { + List list = Arrays.asList(1, 2, 3, 3, 4, 4, 5); + List duplicates = listDuplicate.listDuplicateUsingSet(list); + Assert.assertEquals(duplicates.size(), 2); + Assert.assertEquals(duplicates.contains(3), true); + Assert.assertEquals(duplicates.contains(4), true); + Assert.assertEquals(duplicates.contains(1), false); + } + + @Test + public void givenList_whenUsingFrequencyMap_thenReturnDuplicateElements() { + List list = Arrays.asList(1, 2, 3, 3, 4, 4, 5); + List duplicates = listDuplicate.listDuplicateUsingMap(list); + Assert.assertEquals(duplicates.size(), 2); + Assert.assertEquals(duplicates.contains(3), true); + Assert.assertEquals(duplicates.contains(4), true); + Assert.assertEquals(duplicates.contains(1), false); + } + + @Test + public void givenList_whenUsingFilterAndSetAdd_thenReturnDuplicateElements() { + List list = Arrays.asList(1, 2, 3, 3, 4, 4, 5); + List duplicates = listDuplicate.listDuplicateUsingFilterAndSetAdd(list); + Assert.assertEquals(duplicates.size(), 2); + Assert.assertEquals(duplicates.contains(3), true); + Assert.assertEquals(duplicates.contains(4), true); + Assert.assertEquals(duplicates.contains(1), false); + } + + @Test + public void givenList_whenUsingCollectionsFrequency_thenReturnDuplicateElements() { + List list = Arrays.asList(1, 2, 3, 3, 4, 4, 5); + List duplicates = listDuplicate.listDuplicateUsingCollectionsFrequency(list); + Assert.assertEquals(duplicates.size(), 2); + Assert.assertEquals(duplicates.contains(3), true); + Assert.assertEquals(duplicates.contains(4), true); + Assert.assertEquals(duplicates.contains(1), false); + } + + @Test + public void givenList_whenUsingMapAndCollectorsGroupingBy_thenReturnDuplicateElements() { + List list = Arrays.asList(1, 2, 3, 3, 4, 4, 5); + List duplicates = listDuplicate.listDuplicateUsingCollectionsFrequency(list); + Assert.assertEquals(duplicates.size(), 2); + Assert.assertEquals(duplicates.contains(3), true); + Assert.assertEquals(duplicates.contains(4), true); + Assert.assertEquals(duplicates.contains(1), false); + } + +}