-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathMostRepeatedChar.java
66 lines (57 loc) · 1.43 KB
/
MostRepeatedChar.java
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
60
61
62
63
64
65
66
package com.java.strings;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
/*
* Most Repeated Character
* ------------------------
* Write a Java Program to find Most repeated character.
*
* solution:
* Use Hashmap to store the occurrences of the
* character.
* say Given string : malayalam
* m is occurred 2 times
* a is occurred 4 times
* l is occurred 2 times
* y is occurred 1 time
*
* So "m" is the most repeated char
*
*/
public class MostRepeatedChar {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter any String : ");
String str = scanner.nextLine().trim();
HashMap<Character, Integer> charMap = new HashMap<>();
for(Character c : str.toCharArray()){
if(charMap.containsKey(c))
charMap.put(c, charMap.get(c)+1);
else
charMap.put(c, 1);
}
int maxCount = 0 ;
char mostRepeatedChar = 0;
Set<Character> keySet = charMap.keySet();
for(Character c : keySet){
int value = charMap.get(c);
if(maxCount < value){
maxCount = value;
mostRepeatedChar = c;
}
}
System.out.println("Most Repeated Character is : "+mostRepeatedChar);
System.out.println("Repeated count is : "+maxCount);
scanner.close();
}
}
/*
OUTPUT
Enter any String : malayalam
Most Repeated Character is : a
Repeated count is : 4
Enter any String : java
Most Repeated Character is : a
Repeated count is : 2
*/