-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMost Frequent Digit
More file actions
56 lines (53 loc) · 1.51 KB
/
Most Frequent Digit
File metadata and controls
56 lines (53 loc) · 1.51 KB
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
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import java.util.*;
import java.io.*;
class HelloWorld {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int input1=sc.nextInt();
int input2=sc.nextInt();
int input3=sc.nextInt();
int input4=sc.nextInt();
String s1=String.valueOf(input1);
String s2=String.valueOf(input2);
String s3=String.valueOf(input3);
String s4=String.valueOf(input4);
String s=s1+s2+s3+s4;
char[] ch=s.toCharArray();
Map<Character,Integer> m1=new HashMap<>();
for(int i=0;i<ch.length;i++)
{
if(m1.containsKey(ch[i]))
{
int count=m1.get(ch[i]);
count=count+1;
m1.put(ch[i],count);
}
else
{
m1.put(ch[i],1);
}
}
int maxcount=0;
char maxkey='0';
int maxk=(int)maxkey;
for(Map.Entry<Character,Integer> m2:m1.entrySet())
{
char key=m2.getKey();
int value=m2.getValue();
int ke=(int)key;
if(value>maxcount)
{
maxcount=value;
maxkey=key;
}else if(maxcount==value){
if(ke>maxk){
maxkey=key;
}
}
}
int result = Character.getNumericValue(maxkey);
System.out.println(result);
}
}