Posts

Showing posts with the label Find Maximum Occurring Character in a String

Java Program to Find Maximum Occurring Character in a String

Image
In this section, we will show you how to find m aximum occurring character in a string . 1.  Using For loop and Map 2.  Using For loop 3.  Using Java 9 chars() method 4. Using  Java 8 Streams Example 1. Using For loop and Map Note:  We may have more than one character with the same maximum  occurence . This program will print all the character with maximum  occurence .   import java.util.Collections ; import java.util.HashMap ; import java.util.Map ; public class Main { public static void main ( String [] args) { String string = "java awesome dude" ; char chars [] = string .toCharArray(); Map < Character , Integer > occurrences = new HashMap<>(); for ( Character character : chars ) { Integer oldCount = occurrences .get( character ); if ( oldCount == null ) { oldCount = 0 ; } occurrences .put( character , oldCount + 1 ); } int maxValue =( Collections