这个问题在CSDN上的上镜率比较高,就是写一个程序判断一个数组中出现次数最多的那个元素。我给出的代码是:
- import java.util.*;
- public class FindMostEle {
- private static LinkedHashMap<String, Integer> map;
- public static LinkedHashMap<String, Integer> mostEle(String[] strArray){
- map = new LinkedHashMap<String, Integer>();
- String str = "";
- int count = 0;
- int result = 0;
- for(int i=0; i<strArray.length; i++)
- str += strArray[i];
- for(int i=0; i<strArray.length; i++){
- String temp = str.replaceAll(strArray[i], "");
- count = (str.length() - temp.length())/strArray[i].length();
- if (count > result){
- map.clear();
- map.put(strArray[i], count);
- result = count;
- }
- else if(null == map.get(strArray[i]) && count == result)
- map.put(strArray[i], count);
- }
- return map;
- }
- public static void main(String args[]){
- String[] strArray = {"11", "11", "2", "2", "4", "5", "4"};
- LinkedHashMap<String, Integer> result = mostEle(strArray);
- ArrayList<Integer> c = new ArrayList<Integer>(result.values());
- Set<String> s = result.keySet();
- System.out.print("一共有"+ result.size() +"元素最多。它们分别是");
- System.out.print(s);
- System.out.println(",分别出现了"+ c.get(0) +"次。");
- }
- }
结果是:
- 一共有3元素最多。它们分别是[11, 2, 4],分别出现了2次。