Gokul UK
Gokul UK

Reputation: 49

(Java Code) Return maximum occurring character in an input string

Can anyone please explain the process of this for loop which comes first in the code. If you print these two lines in the console, you get output [0 0 0 1 2]. I don't know "how it works behind the scene to increment the character count every time".

for (int i=0; i<len; i++) 
 count[str.charAt(i)]++; 

//Code
     public class GFG  
        { 
            static final int ASCII_SIZE = 256; 
            static char getMaxOccuringChar(String str) 
            { 
                // Create array to keep the count of individual 
                // characters and initialize the array as 0 
                int count[] = new int[ASCII_SIZE]; 

                // Construct character count array from the input 
                // string. 
                int len = str.length(); 
                for (int i=0; i<len; i++)   //bit confused lines
                    count[str.charAt(i)]++; 

                int max = -1;  // Initialize max count 
                char result = ' ';   // Initialize result 

                // Traversing through the string and maintaining 
                // the count of each character 
                for (int i = 0; i < len; i++) { 
                    if (max < count[str.charAt(i)]) { 
                        max = count[str.charAt(i)]; 
                        result = str.charAt(i); 
                    } 
                } 

                return result; 
            } 

            // Driver Method 
            public static void main(String[] args) 
            { 
                String str = "abcaa"; 
                System.out.println("Max occurring character is " + 
                                    getMaxOccuringChar(str)); 
            } 
        } 

Upvotes: 0

Views: 1929

Answers (3)

Ankit_Singh
Ankit_Singh

Reputation: 1

Maximum occurring character in a single input string

public static char getMaxOccuringChar(String str) {
    char result = ' ';
    int len = str.length();
    HashMap<Character, Integer> StrArray = new HashMap<Character, Integer>();
    int val = 1;
    for(int i =0; i<len; i++) {
        char temp = str.charAt(i);
        if(!StrArray.containsKey(temp)) {
            StrArray.put(temp, val);
        }else {
            StrArray.put(temp, StrArray.get(temp)+1);
        }
    }
    int MaxVal = 0;
    for(char i : StrArray.keySet()) {
        if(StrArray.get(i) > MaxVal) {
            result = i;
            MaxVal = StrArray.get(i);
        }
    }
    return result;
}

Upvotes: 0

Owl Surojit
Owl Surojit

Reputation: 183

The for loop iterates over the string, the value of str.charAt(i) is the character of str at the index i.

Since every char corresponds to an int value (see more about that here) count[str.charAt(i)]++; increases the value of the count array at the index of the given char's corresponding int (see the ascii table to see which one that is).

So after the for loop count contains the number of occurences of every ascii character in str.

Upvotes: 3

Miłosz Tomkiel
Miłosz Tomkiel

Reputation: 125

str.charAt(i) return char from str at the i position. Char can be used as index in array for example myarray['c'] because 'c' can be represented as number (see ASCII table).

So basically for(int i=0; i<len; i++) count[str.charAt(i)]++;

is counting how many times the same letter appears in string.

so for input string "aabc" count['a'] = 2 count['b'] = 1 count['c'] = 1

Upvotes: 1

Related Questions