In this tutorial, we will see “How to find Duplicate Elements in an Array using Java 8?”.
Find Duplicate Elements in an Array in Java 8...!!! Click To Tweet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | /** * Find Duplicate Elements in an Array using Java 8 * @author Deepak Verma * */ import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; public class Find_Duplicate_Elements_In_Array_Using_Java8 { public static void main(String[] args) { int[] numbers = {11, 2, 36, 4, 92, 1, 58, 58, 3, 86, 99, 36, 75}; List<Integer> listOfIntegers = Arrays.stream(numbers) .boxed() .collect(Collectors.toList()); System.out.println("Duplicates elements in the array of "+Arrays.toString(numbers)+" are:"); listOfIntegers.stream() .filter(x -> Collections.frequency(listOfIntegers, x) > 1) .distinct() .forEach(System.out::println); } } |
Output:
1 2 3 | Duplicates elements in the array of [11, 2, 36, 4, 92, 1, 58, 58, 3, 86, 99, 36, 75] is: 36 58 |
In this tutorial, we will see “How to find Duplicate Element in an Array using Java 8?”. In order to find duplicates, we are going to use several techniques.
Find Duplicate Elements in an Array in Java 8...!!! Click To Tweet
Do you like this Post? – then check my other helpful posts:
- Double the even / odd numbers of a specified ArrayList using Streams
- Double the numbers of specified ArrayList using Streams