Java 数组中最频繁和最少重复的频繁数

Java 数组中最频繁和最少重复的频繁数,java,arrays,duplicates,frequency,Java,Arrays,Duplicates,Frequency,我试图找到数组中最频繁的数字和最不频繁的重复数字,例如 [7,5,6,4,6,5,5,8,7,0,7,5,2,9,7,9,3,4,6] 这些是上面数组中的重复编号: “7”出现(4次) “5”出现(4次) “6”出现(3次) “4”出现(2次) “7”和“5”是最常见的数字, “4”是最不常见的重复 当我尝试编写代码时,我能够得到数字7,但是我不知道如何实现最不频繁的代码 这是我写的代码: String[] numbers = "7564655870752979346".split("");

我试图找到数组中最频繁的数字和最不频繁的重复数字,例如

[7,5,6,4,6,5,5,8,7,0,7,5,2,9,7,9,3,4,6]

这些是上面数组中的重复编号:

  • “7”出现(4次)
  • “5”出现(4次)
  • “6”出现(3次)
  • “4”出现(2次)
  • “7”和“5”是最常见的数字, “4”是最不常见的重复

    当我尝试编写代码时,我能够得到数字7,但是我不知道如何实现最不频繁的代码

    这是我写的代码:

    String[] numbers = "7564655870752979346".split("");
            String elements = "";
            int count = 0;
            for (String tempElement : numbers) {
                int tempCount = 0;
                for (n = 0; n < numbers.length; n++) {
                    if (numbers[n].equals(tempElement)) {
                        tempCount++;
                        if (tempCount > count) {
                            elements = tempElement;
                            //  System.out.println(elements);
                            count = tempCount;
                        }
                    }
                }
            }
            System.out.println("Frequent number is: " + elements + " It appeared " + count+" times");
    
    String[]number=“7564655870752979346”。拆分(“”);
    字符串元素=”;
    整数计数=0;
    for(字符串元素:数字){
    int tempCount=0;
    对于(n=0;n计数){
    元素=临时元素;
    //系统输出打印项次(元素);
    计数=临时计数;
    }
    }
    }
    }
    System.out.println(“频繁数是:“+元素+”它出现了“+计数+”次数”);
    
    我上面的解决方案只打印出7个,我不知道如何检查最少的副本

    解决方案:

  • 计算每个数字出现的次数。使用计数数组,每个不同的数字一个
  • 在计数=2的计数数组中查找位置

  • 实施。。。这是你的事。但您需要找到一种方法将包含一个数字的
    字符串
    转换为整数。(提示:搜索javadocs!)

    编写一个函数来查找最小重复数(从2开始)

    整个代码将是

    public class X {
    
      public static String findMin(String[] numbers, int counter) {
        int count = 0;
        String elements = "";
        for (String tempElement: numbers) {
          int tempCount = 0;
          for (int n = 0; n < numbers.length; n++) {
            if (numbers[n].equals(tempElement)) {
              tempCount++;
              if (tempCount > counter) {
                count = 0;
                break;
              }
              if (tempCount > count) {
                elements = tempElement;
                //  System.out.println(elements);
                count = tempCount;
              }
            }
          }
          if (count == counter) {
            return elements;
          }
        }
        if (count < counter) {
          return "";
        }
        return elements;
      }
    
      public static void main(String[] args) {
        String[] numbers = "756655874075297346".split("");
        String elements = "";
        int count = 0;
        for (String tempElement: numbers) {
          int tempCount = 0;
          for (int n = 0; n < numbers.length; n++) {
            if (numbers[n].equals(tempElement)) {
              tempCount++;
              if (tempCount > count) {
                elements = tempElement;
                //  System.out.println(elements);
                count = tempCount;
              }
            }
          }
        }
        String x = "";
        int c = 2;
        do {
          x = findMin(numbers, c++);
        } while (x == "");
    
        System.out.println("Frequent number is: " + elements + " It appeared " + count + " times");
    
        System.out.println("Min Frequent number is: " + x + " It appeared " + (c - 1) + " times");
      }
    }
    
    公共类X{
    公共静态字符串findMin(字符串[]数字,整数计数器){
    整数计数=0;
    字符串元素=”;
    for(字符串元素:数字){
    int tempCount=0;
    对于(int n=0;n计数器){
    计数=0;
    打破
    }
    如果(临时计数>计数){
    元素=临时元素;
    //系统输出打印项次(元素);
    计数=临时计数;
    }
    }
    }
    如果(计数==计数器){
    返回元素;
    }
    }
    如果(计数<计数器){
    返回“”;
    }
    返回元素;
    }
    公共静态void main(字符串[]args){
    字符串[]number=“756655874075297346”。拆分(“”);
    字符串元素=”;
    整数计数=0;
    for(字符串元素:数字){
    int tempCount=0;
    对于(int n=0;n计数){
    元素=临时元素;
    //系统输出打印项次(元素);
    计数=临时计数;
    }
    }
    }
    }
    字符串x=“”;
    int c=2;
    做{
    x=findMin(数字,c++);
    }而(x==”);
    System.out.println(“频繁数是:“+元素+”它出现了“+计数+”次数”);
    System.out.println(“最小频繁数是:“+x+”,它出现了“+(c-1)+“次”);
    }
    }
    
    谢谢你的回答,但当我尝试使用这个数字“3620035681863138685”时,它打印出的3是最频繁的,而“8”和“6”出现的次数与“3”一样多。然后,我如何显示所有最高频率,并在上面的数字中选择一个更高的频率,8会更高,因为它高于6和3。对于这种情况,我建议的解决方案是计算所有数字的重复次数并将其存储在一个数组中,然后从该数组中选择所需的数字@金田
    String x = "";
    int c = 2;
    do {
        x = findMin(numbers, c ++);
    } while(x == "");
    
    public class X {
    
      public static String findMin(String[] numbers, int counter) {
        int count = 0;
        String elements = "";
        for (String tempElement: numbers) {
          int tempCount = 0;
          for (int n = 0; n < numbers.length; n++) {
            if (numbers[n].equals(tempElement)) {
              tempCount++;
              if (tempCount > counter) {
                count = 0;
                break;
              }
              if (tempCount > count) {
                elements = tempElement;
                //  System.out.println(elements);
                count = tempCount;
              }
            }
          }
          if (count == counter) {
            return elements;
          }
        }
        if (count < counter) {
          return "";
        }
        return elements;
      }
    
      public static void main(String[] args) {
        String[] numbers = "756655874075297346".split("");
        String elements = "";
        int count = 0;
        for (String tempElement: numbers) {
          int tempCount = 0;
          for (int n = 0; n < numbers.length; n++) {
            if (numbers[n].equals(tempElement)) {
              tempCount++;
              if (tempCount > count) {
                elements = tempElement;
                //  System.out.println(elements);
                count = tempCount;
              }
            }
          }
        }
        String x = "";
        int c = 2;
        do {
          x = findMin(numbers, c++);
        } while (x == "");
    
        System.out.println("Frequent number is: " + elements + " It appeared " + count + " times");
    
        System.out.println("Min Frequent number is: " + x + " It appeared " + (c - 1) + " times");
      }
    }