-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChallenge.java
More file actions
45 lines (36 loc) · 1.14 KB
/
Challenge.java
File metadata and controls
45 lines (36 loc) · 1.14 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
import java.util.*;
class Challenge {
public static void findIt(String str) {
int max=1;
LinkedHashMap<Character, Integer> hm
= new LinkedHashMap<Character,Integer>();
char[] charArray = str.replaceAll(" ", "").toLowerCase().toCharArray();
for (Character ch : charArray) {
if (hm.containsKey(ch)) {
hm.put(ch, hm.get(ch) + 1);
if(hm.get(ch)>max)
{
max=hm.get(ch);
}
} else {
hm.put(ch, 1);
}
}
for (Map.Entry<Character,Integer> mapElement :
hm.entrySet()) {
Character key = mapElement.getKey();
Integer value = mapElement.getValue();
if(value==max)
{
System.out.println(key);
break;
}
}
}
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter username :");
String userName = myObj.nextLine();
findIt(userName);
}
}