How to find Duplicate Element in an Array of Integers?Java Programming Language
Search Results Featured snippet from the web Java array is an object which contains elements of a similar data type. Additionally, The elements of an array are stored in a contiguous memory location. It is a data structure where we store similar elements. We can store only a fixed set of elements in a Java array . Example 1: int a[]={2,6 ,7,2,8,6}; Here 2 and 6 are the duplicate elements. Example 2: int a[]={5,3,6,2,6,9,5} Here 6 and 5 are the duplicate elements. Source Code: import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; //Brute force approach public class DuplicateInArray { public static void Duplicate(int a[]){ for(int i=0;i<a.length-1;i++){ for(int j=i+1;j<a.length;j++){ if(a[i]==a[j]){ //if above condition is true then element is duplicate and then we print it System.out.println(a[j]); }