找出字符串中出現(xiàn)次數(shù)最多的字符和次數(shù)

題目:找出字符串中出現(xiàn)次數(shù)最多的字符和次數(shù)。
例如:“abbcdaaca”中出現(xiàn)次數(shù)最多的字符就是a,出現(xiàn)次數(shù)是4

思路

由于需要找出字符和次數(shù),這里可以利用Map鍵值對(duì)的形式來(lái)存儲(chǔ),先通過(guò)遍歷字符串并比較字符,如果Map中已經(jīng)存在該字符就修改相對(duì)應(yīng)的value值,也就是每次發(fā)現(xiàn)相同的字符就value++,這樣就得出了字符串中所有字符以及相對(duì)應(yīng)出現(xiàn)的次數(shù)的鍵值對(duì)對(duì)象。最后通過(guò)遍歷Map和比較找出出現(xiàn)次數(shù)最多的字符即可。

 HashMap<Character,Integer> map = new HashMap<Character,Integer> ();
//獲取到字符串的字符和出現(xiàn)的次數(shù)的鍵值對(duì)對(duì)象
        for(int i=0;i<str.length();i++)
        {
            char temp = str.charAt(i);
            if(map.containsKey(temp))
            {
                map.put(temp,map.get(temp)+1);//如果map中已經(jīng)存在就重新改變對(duì)應(yīng)的value值+1
            }
            else
            {
                map.put(temp,1);
            }
        }

獲取到Map對(duì)象通過(guò)遍歷比較找出出現(xiàn)次數(shù)最對(duì)的鍵值對(duì)

 int count = 0;
 Character temp = null;
 for (Map.Entry<Character,Integer> entry:map.entrySet()
             ) {

          if(entry.getValue()>count)
          {
                count = entry.getValue();
                temp = entry.getKey();  
          }
}
 System.out.println("出現(xiàn)最多次的字符是:"+temp);
 System.out.println("出現(xiàn)次數(shù):"+count);

有個(gè)問(wèn)題要解決

如果字符串是“aaaabbbbcc”,出現(xiàn)次數(shù)相同并且都是最多的那怎么辦呢?
這里我覺(jué)得可以再創(chuàng)建一個(gè)teamMap來(lái)存放出現(xiàn)次數(shù)最多有多個(gè)的情況,然后每次返回結(jié)果前就去看teamMap是否存在兩個(gè)或多于兩個(gè)的情況,存在的話就都打印出來(lái)。

         int count = 0;
         Character temp = null;
          //存放臨時(shí)結(jié)果的map
         HashMap<Character,Integer> tempMap = new HashMap<Character, Integer>();
       for (Map.Entry<Character,Integer> entry:map.entrySet()
             ) {

            if(entry.getValue()>count&&tempMap.isEmpty())
            {
                count = entry.getValue();
                temp = entry.getKey();
                tempMap.put(temp,count);
            }
            else if (entry.getValue()>count&&!tempMap.isEmpty())
            {
                tempMap.clear();//存在次數(shù)更多的字符,直接清空之前的元素
                count = entry.getValue();
                temp = entry.getKey();
                tempMap.put(entry.getKey(),entry.getValue());
            }
            else if(entry.getValue()==count)
            {
                tempMap.put(entry.getKey(),entry.getValue());
            }


        }
        if(tempMap.size()>1)
        {

            System.out.println("出現(xiàn)最多次數(shù)的字符有多個(gè)法,分別是:");
            for (Map.Entry<Character,Integer>entry:tempMap.entrySet()
                 ) {
                System.out.println(entry.getKey());
            }
        }
        else
        {
            System.out.println("出現(xiàn)最多次的字符是:"+temp);
        }

完整代碼

public static void getMaxTimeChar(String str)
    {
        HashMap<Character,Integer> map = new HashMap<Character,Integer> ();

        //獲取到字符串的字符和出現(xiàn)的次數(shù)的鍵值對(duì)對(duì)象
        for(int i=0;i<str.length();i++)
        {
            char temp = str.charAt(i);
            if(map.containsKey(temp))
            {
                map.put(temp,map.get(temp)+1);//如果map中已經(jīng)存在就重新改變對(duì)應(yīng)的value值+1
            }
            else
            {
                map.put(temp,1);
            }
        }

        int count = 0;
        Character temp = null;
        HashMap<Character,Integer> tempMap = new HashMap<Character, Integer>();
        for (Map.Entry<Character,Integer> entry:map.entrySet()
                ) {

           /* System.out.println(entry.getKey());
            System.out.println(entry.getValue());*/

            if(entry.getValue()>count&&tempMap.isEmpty())
            {
                count = entry.getValue();
                temp = entry.getKey();
                tempMap.put(temp,count);
            }
            else if (entry.getValue()>count&&!tempMap.isEmpty())
            {
                tempMap.clear();
                count = entry.getValue();
                temp = entry.getKey();
                tempMap.put(entry.getKey(),entry.getValue());
            }
            else if(entry.getValue()==count)
            {
                tempMap.put(entry.getKey(),entry.getValue());
            }


        }
        if(tempMap.size()>1)
        {

            System.out.println("出現(xiàn)最多次數(shù)的字符有多個(gè),分別是:");
            for (Map.Entry<Character,Integer>entry:tempMap.entrySet()
                    ) {
                System.out.println(entry.getKey());
            }
            System.out.println("出現(xiàn)次數(shù)是:"+count);
        }
        else
        {
            System.out.println("出現(xiàn)最多次的字符是:"+temp+">>>>>>>>出現(xiàn)次數(shù):"+count);

        }
    }

引申:如果要找出第二多字符怎么實(shí)現(xiàn)呢?

無(wú)外乎就是在遍歷的時(shí)候再多進(jìn)行一次比較,可以分別創(chuàng)建最大和第二的兩個(gè)變量來(lái)存儲(chǔ),然后在比較的時(shí)候再增加一層else if.

    int maxCount = 0
    Character maxChar= null;
    int secCount = 0;
    Character secChar= null;
    if(entry.getValue>maxCount)
    {
        maxCount = entry.getValue();
        maxChar = entry.getKey();
    }else if(entry.getValue>secCount)
    {
        secCount  = entry.getValue();
        secChar = entry.getKey();
    }

上次面試的時(shí)候被問(wèn)到了,所以在這里做個(gè)記錄。加油。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)載自:https://halfrost.com/go_map_chapter_one/ https://half...
    HuJay閱讀 6,484評(píng)論 1 5
  • 第2章 基本語(yǔ)法 2.1 概述 基本句法和變量 語(yǔ)句 JavaScript程序的執(zhí)行單位為行(line),也就是一...
    悟名先生閱讀 4,613評(píng)論 0 13
  • 1 Object 對(duì)象 教程:https://wangdoc.com/javascript/stdlib/obje...
    智勇雙全的小六閱讀 2,513評(píng)論 0 0
  • 親愛(ài)的姑娘, 你在遠(yuǎn)方的路上, 茉莉花的清香是日記里你寫(xiě)下的夢(mèng)鄉(xiāng); 親愛(ài)的姑娘, 你在前行的路上, 絢爛的色彩是你...
    淘氣的皮皮閱讀 325評(píng)論 0 2
  • 1. 少年不識(shí)愁滋味,為賦新詞強(qiáng)說(shuō)愁,這是小時(shí)候最不喜歡聽(tīng)到的一句話。 另一句是,小小年紀(jì),哪有什么腰。 2. 小...
    希言希語(yǔ)閱讀 643評(píng)論 0 1

友情鏈接更多精彩內(nèi)容