Wednesday, 1 August 2018

Find the HighestOccurence in String

public class HighestOccurence {
    public static void main(String args[]) {
     
        String str = "sushil";
        char[] chars = str.toCharArray();
        int[] arr = new int[26];// number of char possible
        int maxCount = 0;
        char c = 0;
        for (int i = 0; i < chars.length; i++) {
            int index = chars[i] - 'a'; // reduce offset
            arr[index] = arr[index] + 1;

            if (arr[index] > maxCount) {
                maxCount = arr[index];
                c = chars[i];
            }
        }
        System.out.println(String.valueOf(c));
    }
   


output: s

No comments:

Post a Comment

links for Data Structure

  1) 𝐁𝐞𝐜𝐨𝐦𝐞 𝐌𝐚𝐬𝐭𝐞𝐫 𝐢𝐧 𝐋𝐢𝐧𝐤𝐞𝐝 𝐋𝐢𝐬𝐭:  https://lnkd.in/gXQux4zj 2) 𝐀𝐥𝐥 𝐭𝐲𝐩𝐞𝐬 𝐨𝐟 𝐓𝐫𝐞𝐞 𝐓𝐫𝐚𝐯𝐞𝐫𝐬𝐚𝐥𝐬...