forked from aditya109/git-osp-for-beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create HighestFrequencyCharacter.java (aditya109#517)
- Loading branch information
1 parent
4588f76
commit dd852eb
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
Programming/Java/HighestFrequencyCharacter/HighestFrequencyCharacter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.company; | ||
import java.util.HashMap; | ||
import java.util.Scanner; | ||
|
||
//Highest Frequency Character | ||
|
||
public class HighestFrequencyCharacter { | ||
public static void main(String[] args) { | ||
|
||
Scanner sc = new Scanner(System.in); | ||
String str = sc.nextLine(); | ||
|
||
HashMap<Character, Integer> hm = new HashMap<>(); | ||
for (int i = 0; i < str.length(); i++){ | ||
char ch = str.charAt(i); | ||
|
||
if (hm.containsKey(ch)){ | ||
int of = hm.get(ch); | ||
int nf = of+1; | ||
hm.put(ch,nf); | ||
} | ||
else{ | ||
hm.put(ch,1); | ||
} | ||
} | ||
|
||
char mfc = str.charAt(0); | ||
for (Character key : hm.keySet()){ | ||
if(hm.get(key)>hm.get(mfc)){ | ||
mfc=key; | ||
} | ||
} | ||
System.out.println(mfc); | ||
|
||
} | ||
} |