w3resource

Java: Find the second most frequent character in a given string


34. Second Most Frequent Character

Write a Java program to find the second most frequent character in a given string.

Visual Presentation:

Java String Exercises: Find the second most frequent character in a given string


Sample Solution:

Java Code:

// Import necessary Java utilities.
import java.util.*;

// Define a class named Main.
public class Main {
    // Define a constant representing the number of characters (ASCII).
    static final int NOOFCHARS = 256;

    // Define a method to find the second most frequent character in a string.
    static char get2ndMostFreq(String str1) {
        // Create an array to count occurrences of each character in the string.
        int[] ctr = new int[NOOFCHARS];
        int i;

        // Count occurrences of each character in the string.
        for (i = 0; i < str1.length(); i++) {
            (ctr[str1.charAt(i)])++;
        }

        // Variables to store indices of the first and second most frequent characters.
        int ctr_first = 0, ctr_second = 0;
        for (i = 0; i < NOOFCHARS; i++) {
            if (ctr[i] > ctr[ctr_first]) {
                ctr_second = ctr_first;
                ctr_first = i;
            } else if (ctr[i] > ctr[ctr_second] && ctr[i] != ctr[ctr_first]) {
                ctr_second = i;
            }
        }

        // Return the second most frequent character.
        return (char) ctr_second;
    }

    // The main method to execute the code.
    public static void main(String args[]) {
        // Define the input string.
        String str1 = "successes";
        System.out.println("The given string is: " + str1);

        // Get the second most frequent character from the input string.
        char res = get2ndMostFreq(str1);

        // Display the result.
        if (res != '\0') {
            System.out.println("The second most frequent char in the string is: " + res);
        } else {
            System.out.println("No second most frequent character found in the string.");
        }
    }
}

Sample Output:

The given string is: successes
The second most frequent char in the string is: c

Flowchart:

Flowchart: Java String Exercises - Find the second most frequent character in a given string



For more Practice: Solve these Related Problems:

  • Write a Java program to find the second most frequent character in a string, ignoring case.
  • Write a Java program to determine the two most frequent characters in a string and print them in order.
  • Write a Java program to identify the second most frequent character in a string using a HashMap.
  • Write a Java program to find the second most frequent alphanumeric character in a string after filtering out special characters.

Go to:


PREV : All Interleavings of Strings.
NEXT : String Permutations with Repetition.

Java Code Editor:

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.