public int mostNum(int[] array) {
Arrays.sort(array);
int count = 1;
int longest = 0;
int most = 0;
for (int i = 0; i < array.length - 1; i++) {
if (array[i] == array[i + 1]) {
count++;
} else {
count = 1;// 如果不等于,就换到了下一个数,那么计算下一个数的次数时,count的值应该重新符值为一
continue;
}
if (count > longest) {
most = array[i];
longest = count;
}
}
System.out.print("出现次数:" + longest);// 打印出这个数出现的次数已判断是否正确
System.out.println(" ");
System.out.print("众数为:");
return most;
}这个是一个求众数的算法,可是当两个或两个以上的数出现的次数是一样的,比如这种情况 5,2,2,2,4,4,4,6,6;三个2,三个4的时候由于count是大于longest才做赋值操作,那最后返回的结果是2,而不会有4,请问这个算法可以怎么改进,或者有什么其他更好的方法,能把所有的众数打印出来

解决方案 »

  1.   

    用HashMap很简单的public class TestMap                           

    public static void main(String[] args){
    Map<Integer, Integer> map=new HashMap<Integer, Integer>();
    int[] data=new int[]{2,3,4,5,3,4,2,6,7,8,4,5,6,6};
    for(int i=0;i<data.length;i++){
    if((map.get(data[i]))==null){
    map.put(data[i], 1);
    }
    else{
    int num=map.get(data[i]);
    map.put(data[i], num+1);
    }
    }
    Iterator<Integer> it=map.keySet().iterator();
    while(it.hasNext()){
    int num=it.next();
    System.out.println("数字"+num+"出现的次数是"+map.get(num));
    }
    }}
      

  2.   

    package com.seven.most;import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.Arrays;public class Most {
    public static void main(String args[]) {
    int x = 0;
    int i = 0;
    int array[] = new int[10]; Most m = new Most(); System.out.println("输入10个数:");
    for(int j=0; j<=9; j++){
    x = m.getNumFromConsole();
    array[j] = x;
    }
    //Arrays.sort(array); System.out.println(" ");
    System.out.println("=============================");
    System.out.println("您输入的数列为:");

    for (int j = 0; j <= 9; j++) {
    System.out.print(array[j] + ", ");
    } System.out.println(" ");
    System.out.print("众数为: "); m.mostNum(array);
    } public void mostNum(int[] array) {
    ArrayList list = new ArrayList();
    Arrays.sort(array);
    int count = 1;
    int count2 = 1;
    int longest = 0;
    int most = 0;
    for (int i = 0; i < array.length - 1; i++) {
    if (array[i] == array[i + 1]) {
    count++;
    } else {
    count = 1;
    //continue;
    }
    if (count > longest) {
    longest = count;
    }
    }

    for (int i = 0; i < array.length - 1; i++) {
    if (array[i] == array[i + 1]) {
    count2++;
    } else {
    count2 = 1;
    //continue;
    }
    if (count2 == longest) {
    System.out.print(array[i] + ",");
    }
    }

    //System.out.print("众数为:" + most);
    //System.out.print(list);
    System.out.println(" ");
    System.out.print("出现次数:" + longest);// 打印出这个数出现的次数已判断是否正确
    }

    public int getNumFromConsole() {
    String str = null;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try {
    str = br.readLine();
    } catch (IOException ioe) {
    System.out.println(ioe.getMessage());
    }
    return Integer.parseInt(str);
    }
    }