Answers for "find duplicates in list number of duplicates in 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
1

java find duplicates in array

// Uses a set, which does not allow duplicates 

for (String name : names) 
{
     if (set.add(name) == false) 
     {
        // print name your duplicate element
     }
}
Posted by: Guest on May-09-2020

Code answers related to "find duplicates in list number of duplicates in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language