Skip to content

Commit 0d4a7c8

Browse files
committed
hashmap
1 parent 40051b1 commit 0d4a7c8

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
```java
2+
import java.util.*;
3+
4+
class Main {
5+
public static void main(String[] args) {
6+
Scanner sc = new Scanner(System.in);
7+
8+
int n = sc.nextInt();
9+
int[] arr = new int[n];
10+
11+
for (int i=0;i < n;i++) {
12+
arr[i] = sc.nextInt();
13+
}
14+
15+
System.out.println(makeUnique(arr,n));
16+
17+
}
18+
static int makeUnique (int[] arr,int n) {
19+
//Write your code here
20+
HashMap<Integer,Integer> map = new HashMap<>();
21+
22+
for(int val:arr){
23+
map.put(val,map.getOrDefault(val,0)+1);
24+
}
25+
26+
int max = Integer.MIN_VALUE;
27+
for(Map.Entry<Integer,Integer>entry:map.entrySet()){
28+
max = Math.max(max,entry.getValue());
29+
}
30+
return n-max;
31+
}
32+
33+
}
34+
35+
36+
```
37+
38+
39+
Equality in Array
40+
Given an array of integers, determine the minimum number of elements to delete to leave only elements of equal value.
41+
42+
Input Format
43+
The first line contains an integer n, the number of elements in arr.
44+
45+
The next line contains n space-separated integers arr[i].
46+
47+
Output Format
48+
A single integer denoting the minimum number of deletions.
49+
50+
Example 1
51+
Input
52+
53+
4
54+
1 2 2 3
55+
Output
56+
57+
2
58+
Explanation
59+
60+
Delete the 2 elements 1 and 3 leaving arr as [2,2]. If both twos plus either the 1 or the 3 are deleted, it takes 3 deletions to leave either [3] or [1]. The minimum number of deletions is 2.
61+
62+
Example 2
63+
Input
64+
65+
5
66+
3 3 2 1 3
67+
Output
68+
69+
2
70+
Explanation
71+
72+
Delete 2 and 1 to leave [3,3,3]. This is minimal. The only other options are to delete 4 elements to get an array of either [1] or [2].
73+
74+
Constraints
75+
0<n<=100
76+
77+
1<=arr[i]<=100

0 commit comments

Comments
 (0)