Distinct Vals (Hashing)
Problem Statement
Given an array Arr of N elements. Find the minimum number of elements that you need to delete from this array such that all the elements in the array become distinct.
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
HashSet<Integer> hs=new HashSet<>();
for(int i=0;i<n;i++)
{
int num=sc.nextInt();
hs.add(num);
}
System.out.print(n-hs.size()+" ");
}
}
No comments:
Post a Comment