-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrequencyOfDigits.java
More file actions
59 lines (59 loc) · 964 Bytes
/
Copy pathFrequencyOfDigits.java
File metadata and controls
59 lines (59 loc) · 964 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
51
52
53
54
55
56
57
58
59
/*
Q15: Frequency of Digits [Medium]
- Input: 112233
Output: 1→2, 2→2, 3→2
- Input: 1002003
Output: 0→3, 1→1, 2→1, 3→1
*/
package week2;
import java.util.Scanner;
public class FrequencyOfDigits {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int arr[] = new int[10];
int digit;
while(n >0) {
digit = n % 10;
switch(digit) {
case 0:
arr[0]++;
break;
case 1:
arr[1]++;
break;
case 2:
arr[2]++;
break;
case 3:
arr[3]++;
break;
case 4:
arr[4]++;
break;
case 5:
arr[5]++;
break;
case 6:
arr[6]++;
break;
case 7:
arr[7]++;
break;
case 8:
arr[8]++;
break;
case 9:
arr[9]++;
break;
}
n /=10;
}
for(int i=0; i<=9; i++) {
if(arr[i] != 0) {
System.out.print(i+"->"+ arr[i]+" ");
}
}
sc.close();
}
}