如何打印出数组中出现最频繁的所有元素。

huangapple go评论103阅读模式
英文:

How to print out all elemets that is most frequent elemets in a array

问题

以下是翻译好的内容:

  1. 我需要打印出数组中出现次数最多的数字假设我有一个如下的 ```int``` 数组
  2. int[] array = {1, 2, 3, 3, 3, 5}
  3. 然后我需要打印出 3因为它是出现次数最多的元素但是我还需要打印出作为频繁出现的元素假设我现在有这个数组
  4. int[] array = {1, 2, 3, 3, 3, 5, 5, 5, 6, 7, 7, 7}
  5. 然后我需要打印出有序的35 7因为这三个元素一起是出现次数最多的因此输出需要像这样
  6. 3
  7. 5
  8. 7
  9. 这是我目前的代码它可以打印出出现次数最多的一个数字):
  10. Arrays.sort(diceSumArray);
  11. int maxCount = 1;
  12. int index = 1;
  13. int r = diceSumArray[0];
  14. for (int i = 1; i < diceSumArray.length; i++) {
  15. if (diceSumArray[i] == diceSumArray[i-1]) {
  16. index++;
  17. } else {
  18. if (index > maxCount) {
  19. maxCount = index;
  20. r = diceSumArray[i-1];
  21. }
  22. index = 1;
  23. }
  24. }
  25. // 不确定在哪里放打印语句,或者下面的代码是否必要
  26. System.out.println(r);
  27. if (index > maxCount) {
  28. maxCount = index;
  29. r = diceSumArray[diceSumArray.length -1];
  30. }
英文:

I need to print out the numbers that are the most frequent elements in an array. Say that I have an int array like this:

  1. int[] array = {1, 2, 3, 3, 3, 5}

I will then need to print out 3, as that is the most frequent element. But, I also need to print out the element that appear as frequent. Lets say I have this array now:

  1. int[] array = {1, 2, 3, 3, 3, 5, 5, 5, 6, 7, 7, 7}

I then need to print out (ordered) 3, 5 and 7, as all three off them are the most frequent elements together. So the output needs to look like this:

  1. 3
  2. 5
  3. 7

This is the code I have so far (it works for printing out only one off the most frequent numbers)

  1. Arrays.sort(diceSumArray);
  2. int maxCount = 1;
  3. int index = 1;
  4. int r = diceSumArray[0];
  5. for (int i = 1; i &lt; diceSumArray.length; i++) {
  6. if (diceSumArray[i] == diceSumArray[i-1]) {
  7. index++;
  8. } else {
  9. if (index &gt; maxCount) {
  10. maxCount = index;
  11. r = diceSumArray[i-1];
  12. }
  13. index = 1;
  14. }
  15. }
  16. // not sure where to put the print or if the code below is necessary
  17. System.out.println(r);
  18. if (index &gt; maxCount) {
  19. maxCount = index;
  20. r = diceSumArray[diceSumArray.length -1];
  21. }

答案1

得分: 1

以下是翻译好的内容:

更好的方法是使用HashMap来实现线性时间复杂度,您可以遍历数组,保存每个项的出现次数,并返回出现最多的项的列表:

  1. private static List<Integer> getMostFreq(int[] arr){
  2. List<Integer> list = new ArrayList<>();
  3. if(arr == null || arr.length == 0)
  4. return list;
  5. HashMap<Integer, Integer> map = new HashMap<>();
  6. for(int num : arr)
  7. if(!map.containsKey(num))
  8. map.put(num,1);
  9. else
  10. map.replace(num, map.get(num)+1);
  11. int max = Integer.MIN_VALUE;
  12. for(HashMap.Entry<Integer,Integer> item : map.entrySet())
  13. if(item.getValue() > max)
  14. max = item.getValue();
  15. for(HashMap.Entry<Integer,Integer> item : map.entrySet())
  16. if(item.getValue() == max)
  17. list.add(item.getKey());
  18. return list;
  19. }
英文:

A better way to do this would be using HashMap to achieve a linear time complexity, where you would iterate over the array, save the # of occurrences of each item, and return a list of those that occur the most:

  1. private static List&lt;Integer&gt; getMostFreq(int[] arr){
  2. List&lt;Integer&gt; list = new ArrayList&lt;&gt;();
  3. if(arr == null || arr.length == 0)
  4. return list;
  5. HashMap&lt;Integer, Integer&gt; map = new HashMap&lt;&gt;();
  6. for(int num : arr)
  7. if(!map.containsKey(num))
  8. map.put(num,1);
  9. else
  10. map.replace(num, map.get(num)+1);
  11. int max = Integer.MIN_VALUE;
  12. for(HashMap.Entry&lt;Integer,Integer&gt; item : map.entrySet())
  13. if(item.getValue() &gt; max)
  14. max = item.getValue();
  15. for(HashMap.Entry&lt;Integer,Integer&gt; item : map.entrySet())
  16. if(item.getValue() == max)
  17. list.add(item.getKey());
  18. return list;
  19. }

huangapple
  • 本文由 发表于 2020年4月7日 18:52:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/61078351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定