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

解决方案 »

  1.   

    应该是出现最多的那个数吧?最简单的办法:数字只有0-9,用replaceAll替换 然后计算字符串之前的长度length1和替换之后的长度length2,两者的差就是该数字出现的次数。
      

  2.   

    public class TestCount {
    public static void main(String args[]){
    String s = "3351353558";
    findMost(s);
    }

    public static void findMost(String s){
    int count = 0;
    int most = count;
    char num = '0';

    for(int i = 0; i < s.length(); i++){
    for(int j = i; j < s.length(); j++){
    if(s.charAt(i) == s.charAt(j))
    count++;
    }
    if(count > most){
    most = count;
    num = s.charAt(i);
    }
    count = 0;
    }
    System.out.println(num);
    }
    }
      

  3.   

    写一个,楼主看看,应该效率不错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] + "次");
            }    }}
      

  4.   

    改一点点,所有0-9出现次数都打印出来了,最多的就不用说了。、。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 < 10; i++)
            {
                System.out.println("字符" + (char) ('0' + i) + "出现了" + x[i] + "次");
            }    }}
      

  5.   

    public class Test{
        public static void main(String args[])  throws Exception{
        
    String str="3351353558";
    System.out.println("出现次数最的数字是:"+maxTimesNum(str));    }
        public static int maxTimesNum(String str){
         if(!str.matches("\\d+")){
         new RuntimeException("格式不对");
         }
        
         char maxChar=0;
         int[] times=new int[10];
         int maxIndex=0;
         for(char c: str.toCharArray()){
         times[c-'0']++;
         }
         for(int i=0;i<times.length;i++){
         if(times[i]>times[maxIndex]) maxIndex=i;
         }
         return maxIndex;
        }
    }
      

  6.   


    public class T {
    public static void main(String[] args) {
    int[] data = { 3, 3, 5, 20, 3, 5, 3, 5, 5, 8 };
    int size = 0;
    for (int i = 0; i < data.length; i++) {
    if (data[i] > size) {
    size = data[i];
    }
    } int[] counts = new int[size + 1];
    for (int i = 0; i < data.length; i++) {
    counts[data[i]] += 1;
    } int max = 0;
    for (int i = 0; i < counts.length; i++) {
    if (counts[i] > max) {
    max = counts[i];
    }
    } System.out.println("出现次数最多的有:");
    for (int i = 0; i < counts.length; i++) {
    if (counts[i] == max) {
    System.out.println(i);
    }
    }
    }
    }
      

  7.   

    往map 里放,key 是 0-9, value 是出现次数。
      

  8.   


    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());
    }
    }
    }
    }
      

  9.   

    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());
    }
      

  10.   

    import javax.swing.JOptionPane;
    public class To {
    private static String s=JOptionPane.showInputDialog("Enter a number : ");
    private static char []a=new char[10];
    private static int []b=new int[a.length];

        public static void main(String[] args){
         for(int i=0;i<10;i++)
         a[i]=(char)(i+'0');
        
         for(int i=0;i<s.length();i++)
         for(int j=0;j<10;j++){
         if(s.charAt(i)==a[j]){
         b[j]++;
         break;
         }
         }
        
         int max=b[0],k=0;
         for(int i=1;i<b.length;i++)
         if(b[i]>max){
         max=b[i];
         k=i;
         }
        
         System.out.println("The number  "+k+" occurs "+max+" times ,is the most.");
        }
    }此代码只可以解决N位数中没有出现次数相同的数字;如果存在,以最先出现的为最多。
    如:
       输入   112233
      出现最多的数字为:1 而 2 和 3 不是。
      

  11.   


    二楼的方法真的很简单。String s = 3351353558;char getCh() {
    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;
    }
      

  12.   

    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));
    }
    }楼上的方法确实不错! 不过有个问题没解决:    输入  112233    出现最多的数字为:1 ,而 2 和 3 不是。能不能 再考虑一下?
      

  13.   

    要不方法返回 List<char>,然后if (ntimes > max) {
        max = ntimes;
        list.clear();
        list.add(c);
    } else if (ntimes == max) {
        list.add(c);
    }
      

  14.   

    import java.util.regex.*;
    public class Test
    {
    public static void main(String[] args){
    String s = "ddfdshdddddddnfsbsasxcdhssssdndsfddsjkd";
    char[] c = s.toCharArray();
    int count = 0;char ch = c[0];
    for(int i = 0;i<c.length-1;i++){
    int n=0;
    if(c[i]=='\u0000') continue;
    for(int j=i+1;j<c.length;j++){
    if(c[j]=='\u0000') continue;
    if((c[j]^c[i])==0){
    n++;
    c[j]='\u0000';
    }
    }
    if(n>count){
    count =n;
    ch=c[i];
    }
    }
    System.out.println(count+"\t"+ch);
    }
    }