一组数字 挑选出出现频率最高的3 2 5  2   3    4   2    3 最高的 是 2 和3 

解决方案 »

  1.   

    我的笨方法:import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;public class MaxAppear {

    /**
     * <p>功能描述:执行</p>
     * @param intArray
     * @return
     * @author:jack
     * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    public List<Integer> go(int[] intArray) {
    List<Integer> list = new ArrayList<Integer>();
    Map<Integer, Integer> map = this.init(intArray);
    for(int i : intArray) {
    map.put(i, map.get(i)+1);
    }
    this.calc(map, list);
    return list;
    }

    /**
     * <p>功能描述:计算出现最多的数字,存到List中</p>
     * @param map
     * @param list
     * @author:jack
     * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    private void calc(Map<Integer, Integer> map, List<Integer> list) {
    Integer max = Integer.MIN_VALUE;
    list.add(max);
    for(Entry<Integer, Integer> entry : map.entrySet()) {
    Integer key = entry.getKey();
    Integer value = entry.getValue();
    if(value>max) {
    list.remove(max);
    list.add(key);
    max = value;
    }else if(value==max) {
    list.add(key);
    }
    }
    }

    /**
     * <p>功能描述:把不重复的数字初始化到Map中</p>
     * @param intArray
     * @return
     * @author:jack
     * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    private Map<Integer, Integer> init(int[] intArray) {
    Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    HashSet<Integer> set = new HashSet<Integer>();
    for(int i : intArray) set.add(i);
    for(int i : set) map.put(i, 0);
    return map;
    } public static void main(String[] args) {
    MaxAppear maxAppear = new MaxAppear();
    int[] intArray = new int[]{3, 2, 5, 2, 3, 4, 2, 3 };
    List<Integer> list = maxAppear.go(intArray);
    System.out.println("出现频率最高的是:");
    for(int i : list) {
    System.out.print(i+"    ");
    }
    }}
      

  2.   

    import java.util.HashMap;
    import java.util.Map;
    public class Numbers {
    int[] num = {4,5,6,4,4,2,3,6,2,6,6};
    static int step = 1;
    Map<Integer,Integer> map = new HashMap<Integer,Integer>(); public static void main(String[] args) {
    new Numbers().launch();

    }

    public void launch() {
    for(int i=0;i<num.length;i++) {
    if(!map.containsKey(num[i])) {
    map.put(num[i],step);
    } else {
    int freq = map.get(num[i]);
    map.put(num[i], freq + 1);
    }
    }
    System.out.println(map);
    }}  我的办法,看看有什么不对的
      

  3.   


    public class CompanyTest
    {
    public static void main(String[] args)
    {
    int[] source={3,2,5,2,3,4,2,3};
    int len=source.length;
    int[] maxfreq=new int[len];
    int max=0;
    for(int i=0;i<len;i++)
    {
    maxfreq[source[i]]++;
    if(maxfreq[source[i]]>max)
    max=maxfreq[source[i]];
    }
    for(int i=0;i<len;i++)
    {
    if(maxfreq[i]==max)
    {
    System.out.println(i+"出现"+max+"次");
    }
    }
    }
    }
      

  4.   

    新建一个map,键值放数组的值,对应的值放出现的次数。
    遍历一遍数组能把map填满,再遍历一遍map可以找出出现次数最多的值
      

  5.   


    public class TestMain {    public static void main(String[] arguments) {
         int[] num = {5,1235,8,5,3,5,3,3,3,1,1,1};
         Map<Integer, Integer> map = new HashMap<Integer, Integer>();
         for( int i =0 ; i<num.length ;i++){
         if(map.containsKey(num[i])){
         map.put(num[i], (Integer)map.get(num[i])+1);
         }else{
         map.put(num[i], 1);
         }
        
         }
         Integer max= Integer.MIN_VALUE;
            for( Entry<Integer, Integer> entry : map.entrySet() ) {
                Integer key = entry.getKey();
                Integer value = entry.getValue();
                
                if(value>max){
                 max=value;
                }
                
            }
            
            for( Entry<Integer, Integer> entry : map.entrySet() ) {
                Integer key = entry.getKey();
                Integer value = entry.getValue();
                
                if(value==max){
                 System.out.println("频率最大数"+key+" 频率:"+max);
                }
                
            }
        }
      

  6.   

    http://topic.csdn.net/u/20081023/23/791585fe-585b-4e0a-8dcf-264ab049a4c7.html
    杂跟这一样呢。。
      

  7.   

    public class FrequencyTest { public static void main(String[] args){
    int[] source = { 3 ,2,5 , 2 , 3 ,   4 , 2  ,  3 };
    int[] data = { 0,1,2,3,4,5,6,7,8,9 };
    int[] result = new int[10];
    for(int i=0;i<source.length;i++){
    for(int j=0;j<data.length;j++ ){
    if( source[i] == data[j] ){
    result[j] ++;
    }
    }
    }
    for(int i=0;i<result.length;i++){
    System.out.println(i+"出现的频率为"+result[i]);
    }
    }
    }
      

  8.   

    public static void review() {
    int[] source = { 3, 2, 5, 2, 3, 4, 2, 3 };
    int[] data = { 2, 3, 4, 5 };
    int[] result = new int[10];
    for (int i = 0; i < data.length; i++) { for (int j = 0; j < source.length; j++) {
    if (data[i] == source[j]) {
    result[i]++;
    }
    }
    System.out.println(source[i] + "出现的频率为" + result[i]);
    }
    }