In: Computer Science
In Java, write a program that finds the first character to occur 3 times in a given string?
EX. Find the character that repeats 3 times in "COOOMMPUTERRRR"
/*If you any query do comment in the comment section else like the solution*/
import java.util.HashMap;
import java.util.Scanner;
public class FirstCharacterWithNFrequency {
public static void main(String[]args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
HashMap<Character, Integer> hm = new HashMap<Character, Integer>();
for(int i=0;i<str.length();i++) {
if(hm.containsKey(str.charAt(i))) {
int currfreq = Integer.parseInt(hm.get(str.charAt(i)).toString());
hm.put(str.charAt(i), currfreq + 1);
} else {
hm.put(str.charAt(i), 1);
}
int freq = Integer.parseInt(hm.get(str.charAt(i)).toString());
if(freq == 3) {
System.out.print("First Character encountered with frequency three is: " + str.charAt(i));
break;
}
}
}
}