package com.altafjava.fdeia.test;
import java.util.HashSet;
public class Test2 {
public static void main(String[] args) {
int[] arr={1,6,5,1,2,2,9,6};
System.out.println("Input Array = 1,6,5,1,2,2,9,6");
System.out.print("The repeating elements are : ");
HashSet<Integer> hashSet=new HashSet<>();
for (int i = 0; i < arr.length; i++)
{
if(hashSet.add(arr[i])==false)
System.out.print(arr[i]+" ");
}
}
}
As we know HashSet does not allow duplicate elements. We can get benifit from it. If we add element into HashSet and if it is returning false. It that means it element is already added HashSet and this is duplicate element.
How to find duplicate elements in an array using HashSet? |
0 Comment to "How to find duplicate elements in an array using HashSet?"
Post a Comment