代码之家  ›  专栏  ›  技术社区  ›  WoeIs

在数组中查找最常出现的值,如果存在并列关系,则选择最低值

  •  4
  • WoeIs  · 技术社区  · 7 年前

    我有下面的程序,它查找数组“a”,然后输出数组中出现频率最高的值。然而,我想实现的另一个条件是,在两个不同值出现相等次数的情况下,最小值得到输出。

    所以对于以下带有数组的代码:

    int a[] = {34, 34, 20, 20, 15};
    

    它输出34,但是我希望它输出20,因为这是一个较低的值,在数组中出现的次数相同。

    public class Arrays3 {
        public static void main(String[] args){
            int a[] = {34, 34, 20, 20, 15};
            mode(a);
        }
        public static int mode(int[] a) {
            int[] counts = new int[101];
            int maxCount = 0;
            int maxKey = 0;
    
            for(int i = 0; i < a.length; i++) {
                counts[a[i]]++;
                if(counts[a[i]] > maxCount) {
                    maxCount = counts[a[i]];
                    maxKey = a[i];
                }
            }
            System.out.println(maxKey);
            return maxKey;
        }
    }
    
    3 回复  |  直到 7 年前
        1
  •  2
  •   GBlodgett    7 年前

    你可以核对一下 maxKey 然后按照以下思路做一些事情:

     if(counts[a[i]] == maxCount && counts[a[i]] < maxKey) {
           maxKey = counts[a[i]];
     }
    

    将设置为较小的元素。那么如果 count[a[i]] 比任何时候都大 maxCount , 马克斯基 将被重写并成为最常出现的元素:

    for(int i = 0; i < a.length; i++) {
         counts[a[i]]++;
    
         if(counts[a[i]] > maxCount) {
              maxCount = counts[a[i]];
              maxKey = a[i];
          }
          if(counts[a[i]] == maxCount && counts[a[i]] < maxKey) {
            maxKey = a[i];
          }
    }
    System.out.println(a[maxKey]);
    

    20
    
        2
  •  2
  •   Jacob G.    7 年前

    Map 通过流式处理阵列并使用 Collectors.groupingBy 具有 Collectors.counting() .

    收集器.分组依据 再次创建 Map<Long, SortedSet<Integer>> (键是频率,值是数组中按该频率排序的一组值)。

    地图 SortedSet :

    int[] a = {34, 34, 20, 20, 15};
    
    var lowest = Arrays.stream(a)
                       .boxed()
                       .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
                       .entrySet()
                       .stream()
                       .collect(Collectors.groupingBy(Map.Entry::getValue,
                               Collectors.mapping(Map.Entry::getKey, Collectors.toCollection(TreeSet::new))))
                       .entrySet()
                       .stream()
                       .sorted(Comparator.comparing(Map.Entry::getKey, Comparator.reverseOrder()))
                       .map(Map.Entry::getValue)
                       .mapToInt(TreeSet::first)
                       .findFirst();
    
    lowest.ifPresent(System.out::println);
    

    20
    
        3
  •  0
  •   Dang Nguyen    7 年前
    • 你想求最小值,所以我建议 int maxKey = Integer.MAX_VALUE;
    • 然后每次发现更好的计数时,我们都必须进行比较以更新maxKey

      int[] counts = new int[101];
      int maxCount = 0;
      int maxKey = Integer.MAX_VALUE;
      
      for(int i = 0; i < a.length; i++) {
          counts[a[i]]++;
          if(counts[a[i]] >= maxCount) {
              maxCount = counts[a[i]];
              if(a[i] < maxKey) maxKey = a[i];
      
          }
      }
      System.out.println(maxKey);
      return maxKey;
      

    享受编码的乐趣