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...!!! Share on X

/**
 * 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:

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...!!! Share on X

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts