-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcapgemini_04.java
More file actions
50 lines (32 loc) · 881 Bytes
/
Copy pathcapgemini_04.java
File metadata and controls
50 lines (32 loc) · 881 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
Problem Statement –
You’re given an array of integers, print the number of times each integer has occurred in the array.
Example
Input :
10
1 2 3 3 4 1 4 5 1 2
Output :
1 occurs 3 times
2 occurs 2 times
3 occurs 2 times
4 occurs 2 times
5 occurs 1 times
*/
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class capgemini_04 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
sc.close();
Map<Integer,Integer> m=new HashMap<>();
for(int i:a){
m.put(i,m.getOrDefault(i,0)+1);
}
m.forEach((key, value) -> System.out.println(key + " occurs " + value + " times"));
}
}