Find the most common String in ArrayList()

后端 未结 12 945
无人及你
无人及你 2020-12-08 16:59

Is there a way to find the most common String in an ArrayList?

ArrayList list = new ArrayList<>();
list.add(\"t         


        
12条回答
  •  醉酒成梦
    2020-12-08 17:56

    You could use a HashMap. Looping through the array, you can check for each String if it is not already a Key of your HashMap, add it and set the value to 1, if it is, increase its value by 1.

    Then you have a HashMap with all unique Strings and an associated number stating their amount in the array.

提交回复
热议问题