Answers for "find all duplicates in list of integers java"

2

java 8 get duplicates in list

List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 4, 4);
Set<Integer> nbrRemovedSet = new HashSet<>();

// Set.add() returns false if the element was already in the set.
Set<Integer> nbrSet = numbers
	.stream()
  	.filter(n -> !nbrRemovedSet.add(n))
  	.collect(Collectors.toSet());

// also, we can use Collections.frequency:
Set<Integer> nbrSet = numbers
	.stream()
  	.filter(i -> Collections.frequency(numbers, i) >1)
    collect(Collectors.toSet());
Posted by: Guest on February-18-2021
0

java find duplicate element in list

public static Set<String> findDuplicates(List<String> listContainingDuplicates) {
 
		final Set<String> setToReturn = new HashSet<String>();
		final Set<String> set1 = new HashSet<String>();
 
		for (String yourInt : listContainingDuplicates) {
			if (!set1.add(yourInt)) {
				setToReturn.add(yourInt);
			}
		}
		return setToReturn;
	}
Posted by: Guest on November-14-2020

Code answers related to "find all duplicates in list of integers java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language