Find Duplicates Using HashSet
import java.util.HashSet;
import java.util.Set;
public class DuplicateElements {
public static void main(String[] args) {
int [] arr = {1, 2, 3, 2, 4, 5, 3, 8, 8};
Set <Integer> set = new HashSet<Integer>();
for(int i = 0; i< arr.length;i++)
{
if(set.add(arr[i])==false)
{
System.out.println("Duplicate Element Found :" +arr[i]);
}
}
}
}
import java.util.Set;
public class DuplicateElements {
public static void main(String[] args) {
int [] arr = {1, 2, 3, 2, 4, 5, 3, 8, 8};
Set <Integer> set = new HashSet<Integer>();
for(int i = 0; i< arr.length;i++)
{
if(set.add(arr[i])==false)
{
System.out.println("Duplicate Element Found :" +arr[i]);
}
}
}
}
Comments
Post a Comment