有N个数,例如3351353558,求出最多的那个数

import java.util.ArrayList;    
import java.util.Collections;    
import java.util.HashMap;    
import java.util.List;    
import java.util.Map;    
   
/**   
 *  对字符集中字符出现的次数进行排序。   
 *    
 * @author 赵学庆 www.java2000.net   
 */   
public class T {    
  public static void main(String args[]) {    
    String str = "12345678hfdjkslahfkj932189oiefsjkar94werfdsf";    
    Map<Character, KeyValue> map = new HashMap<Character, KeyValue>();    
    char c;    
    KeyValue kv = null;    
    for (int i = 0; i < str.length(); i++) {    
      c = str.charAt(i);    
      kv = map.get(c);    
      if (kv == null) {    
        kv = new KeyValue();    
        kv.ch = c;    
        kv.count = 1;    
        map.put(c, kv);    
      } else {    
        kv.count++;    
      }    
    }    
    List<KeyValue> list = new ArrayList<KeyValue>(map.values());    
    Collections.sort(list);    
    for (KeyValue o : list) {    
      System.out.println(o.ch + "=" + o.count);    
    }    
  }    
}    
   
class KeyValue implements Comparable {    
  public int compareTo(Object obj) {    
    if (obj instanceof KeyValue) {    
      KeyValue kv = (KeyValue) obj;    
      return kv.count - this.count;    
    }    
    return -1;    
  }    
   
  char ch;    
   
  int count;    
}   

-----------------------------------------------------------------------------------------------------------
public class TestCount
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        String str = "12455547464515475354635";

        int[] x = new int[10];
        char[] ca = str.toCharArray();
        for (int i = 0; i < ca.length; i++)
        {
            x[ca[i] - '0']++;
        }

        for (int i = 0; i < x.length; i++)
        {
            System.out.println("字符" + (char) ('0' + i) + "出现了" + x[i] + "次");
        }

    }

}

--------------------------------------------------------------------------------------
  
 public static void main(String[] args) 
{
        int[] data = { 3, 3, 5, 1, 3, 5, 3, 5, 5, 8 };
        Map<Integer, Integer> m = new HashMap<Integer, Integer>();
        for (int i = 0; i < data.length; i++) 
       {
            if (m.get(data[i]) == null) {
                m.put(data[i], 1);
            } else {
                m.put(data[i], m.get(data[i]) + 1);
            }
        }
        System.out.println("Map里的元素: " + m);

        int t = 0;
        Set<Map.Entry<Integer, Integer>> set = m.entrySet();
        for (Entry<Integer, Integer> entry : set) {
            if (entry.getValue() > t) {
                t = entry.getValue();
            }
        }

        System.out.println("次数最多的有:");
        for (Entry<Integer, Integer> entry : set) {
            if (entry.getValue() == t) {
                System.out.println(entry.getKey());
            }
        }
    }
}
---------------------------------------------------------------------------------




public static void main(String[] args) 
{
        String str = "12455547464515475354635";
        String out = "";

        char[] ca = str.toCharArray();

        Arrays.sort(ca);

        for (char c : ca) 
        {
            out += c;
        }

        Matcher m = Pattern.compile("(\\d)\\1*").matcher(out);

        while (m.find())
            System.out.println(m.group().charAt(0) + "的个数:"
                    + m.group().length());
    }

----------------------------------------------------------------------



import javax.swing.*; 

public class To { 
private static String s = JOptionPane.showInputDialog("Enter a Integer number: "); 

public static char getCh(String s) { 
    int max = 0; 
    char ch=' '; 

    for (int i = 0; i  < s.length(); i++) { 
    char c = s.charAt(i); 

    int ntimes = s.length() - s.replaceAll(Character.toString(c), "").length(); 
    if (ntimes > max) { 
    max = ntimes; 
    ch = c; 
    } 
    } 

    return ch; 
} 

public static void main(String[] args){ 
System.out.println("The number is : "+getCh(s)); 
} 
} 
---------------------------------------------------------------------------

import javax.swing.*; 

public class To { 
private static String s = JOptionPane.showInputDialog("Enter a Integer number: "); 

public static char getCh(String s) { 
    int max = 0; 
    char ch=' '; 

    for (int i = 0; i < s.length(); i++) { 
    char c = s.charAt(i); 
    
    int ntimes = s.length() - s.replaceAll(Character.toString(c), "").length(); 
    if (ntimes > max) { 
    max = ntimes; 
    ch = c; 
    } 
    } 
    
    return ch; 
} 

public static void main(String[] args){ 
System.out.println("The number is : "+getCh(s)); 
} 
}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lz12366/archive/2009/10/05/4633275.aspx

你可能感兴趣的:(java,C++,c,swing,C#)