Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions highest_freq_char.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.util.*;
//Question : Suppose String = "abracadabra" , we need to find the string having highest frequency , suppose in this case ,'a'
public class highestFreqString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a String : ");
String s= sc.next();
HashMap<Character,Integer> hm = new HashMap<>();

for(int i = 0 ; i < s.length() ; i++){
char word = s.charAt(i);
if(hm.containsKey(word)){
int oldFreq = hm.get(word);
int newFreq = oldFreq + 1;
hm.put(word , newFreq);
}else{
hm.put(s.charAt(i) , 1);
}

}

char max = s.charAt(0);
for(char key : hm.keySet()){
if(hm.get(key) > hm.get(max)){
max = key;
}
}
System.out.println(max);




}
}