“数组中多个最常见的元素”

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

Multiple most frequent elements in array

问题

我想检测并在屏幕上打印出出现频率最高的项。我知道当最常见的元素只有一个时该如何做。然而,如果我们有这样一个包含以下元素的列表:

10, 5, 2, 1, 2, 4, 6, 6, 6, 6, 10, 10, 10

我想打印出六和十,我的意思是无论有多少个,我都想打印出所有出现频率最高的元素。

英文:

Suppose we have a list of integers. I would like to detect and print on the screen the most frequently repeated item . I know how to do it when the most common element is only one. However, if we have such a list which contains these elements:

  1. 10, 5, 2, 1, 2, 4, 6, 6, 6, 6, 10, 10, 10

i want to print a six and a ten so I mean I want to print all the most frequently repeated elements no matter how many there are..

答案1

得分: 1

Sure, here's the translated Python code:

  1. nums = [10, 5, 2, 1, 2, 4, 6, 6, 6, 6, 10, 10, 10]
  2. def most_recurrent_ints(arr):
  3. log = {}
  4. for i in arr:
  5. if i in log:
  6. log[i] += 1
  7. else:
  8. log[i] = 1
  9. current_max = 0
  10. for i in log.values():
  11. if i > current_max:
  12. current_max = i
  13. results = []
  14. for k, v in log.items():
  15. if v == current_max:
  16. results.append(k)
  17. return results
  18. print(most_recurrent_ints(nums))

Please note that the code translation is provided as requested, and no additional content or answers are included.

英文:
  1. nums = [10, 5, 2, 1, 2, 4, 6, 6, 6, 6, 10, 10, 10]
  2. def most_recurrent_ints(arr):
  3. log = {}
  4. for i in arr:
  5. if i in log:
  6. log[i] += 1
  7. else:
  8. log[i] = 1
  9. current_max = 0
  10. for i in log.values():
  11. if i > current_max:
  12. current_max = i
  13. results = []
  14. for k, v in log.items():
  15. if v == current_max:
  16. results.append(k)
  17. return results
  18. print(most_recurrent_ints(nums))

I'm bad at Java but this is the Python solution, maybe someone can translate. I can do it in JS if you need.

答案2

得分: 1

以下是翻译好的部分:

你考虑过使用字典类型的数据结构吗?我不懂 Java,所以可能不太优雅,但它实现了你所需的功能:

  1. final int[] array = new int[]{ 10, 5, 2, 1, 2, 4, 6, 6, 6, 6, 10, 10, 10 };
  2. Map<Integer, Integer> dictionary = new HashMap<Integer,Integer>();
  3. for(int i = 0; i < array.length; ++i){
  4. int val = array[i];
  5. if(dictionary.containsKey(val)){
  6. dictionary.put(val, dictionary.get(val) + 1);
  7. }else{
  8. dictionary.put(val, 1);
  9. }
  10. }
  11. for(Map.Entry<Integer,Integer> entry : dictionary.entrySet()){
  12. System.out.println(entry.getKey() + ": " + entry.getValue());
  13. }

这将输出:

  1. 1: 1
  2. 2: 2
  3. 4: 1
  4. 5: 1
  5. 6: 4
  6. 10: 4
英文:

Have you considered using a dictionary-type data structure? I am not a java person, so this may not be pretty, but it does what you are looking for:

  1. final int[] array = new int[]{ 10, 5, 2, 1, 2, 4, 6, 6, 6, 6, 10, 10, 10 };
  2. Map&lt;Integer, Integer&gt; dictionary = new HashMap&lt;Integer,Integer&gt;();
  3. for(int i = 0; i &lt; array.length; ++i){
  4. int val = array[i];
  5. if(dictionary.containsKey(val)){
  6. dictionary.put(val, dictionary.get(val) + 1);
  7. }else{
  8. dictionary.put(val, 1);
  9. }
  10. }
  11. for(Map.Entry&lt;Integer,Integer&gt; entry : dictionary.entrySet()){
  12. System.out.println(entry.getKey() + &quot;: &quot; + entry.getValue());
  13. }

This would output:

  1. 1: 1
  2. 2: 2
  3. 4: 1
  4. 5: 1
  5. 6: 4
  6. 10: 4

答案3

得分: 1

以下是您提供的代码的中文翻译部分:

  1. public static void main(String[] args) {
  2. MostFrequent(new Integer[] {10, 5, 2, 1, 2, 4, 6, 6, 6, 6, 10, 10, 10});
  3. }
  4. static void MostFrequent(Integer[] arr) {
  5. Map<Integer, Integer> count = new HashMap<Integer, Integer>();
  6. for (Integer element : arr) {
  7. if (!count.containsKey(element)) {
  8. count.put(element, 0);
  9. }
  10. count.put(element, count.get(element) + 1);
  11. }
  12. Map.Entry<Integer, Integer> maxEntry = null;
  13. ArrayList<Integer> list = new ArrayList<Integer>();
  14. for (Map.Entry<Integer, Integer> entry : count.entrySet()) {
  15. if (maxEntry == null || entry.getValue() > maxEntry.getValue()) {
  16. list.clear();
  17. list.add(entry.getKey());
  18. maxEntry = entry;
  19. }
  20. else if (entry.getValue() == maxEntry.getValue()) {
  21. list.add(entry.getKey());
  22. }
  23. }
  24. for (Integer item: list) {
  25. System.out.println(item);
  26. }
  27. }

输出结果:

  1. 6
  2. 10
英文:
  1. public static void main(String[] args) {
  2. MostFrequent(new Integer[] {10, 5, 2, 1, 2, 4, 6, 6, 6, 6, 10, 10, 10});
  3. }
  4. static void MostFrequent(Integer[] arr) {
  5. Map&lt;Integer, Integer&gt; count = new HashMap&lt;Integer, Integer&gt;();
  6. for (Integer element : arr) {
  7. if (!count.containsKey(element)) {
  8. count.put(element,0);
  9. }
  10. count.put(element, count.get(element) + 1);
  11. }
  12. Map.Entry&lt;Integer, Integer&gt; maxEntry = null;
  13. ArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();
  14. for (Map.Entry&lt;Integer, Integer&gt; entry : count.entrySet()) {
  15. if (maxEntry == null || entry.getValue() &gt; maxEntry.getValue()) {
  16. list.clear();
  17. list.add(entry.getKey());
  18. maxEntry = entry;
  19. }
  20. else if (entry.getValue() == maxEntry.getValue()) {
  21. list.add(entry.getKey());
  22. }
  23. }
  24. for (Integer item: list) {
  25. System.out.println(item);
  26. }
  27. }

Output:

  1. 6
  2. 10

答案4

得分: 1

以下是翻译好的代码部分:

  1. 你需要一个地图也称为字典数据结构来解决这个问题这是我能想到的最快最简洁的解决方案
  2. public static void main(String[] args){
  3. int[] arr = new int[]{10, 5, 2, 1, 2, 4, 6, 6, 6, 6, 10, 10, 10};
  4. printMostFrequent(arr);
  5. }
  6. private static void printMostFrequent(int[] arr){
  7. // 键:输入数组中的数字
  8. // 值:数字在输入数组中出现的次数
  9. Map<Integer, Integer> counts = new HashMap<>();
  10. // 同一个数字在输入数组中出现的最大次数。在这个例子中,它是4。
  11. int highestFrequency = 0;
  12. // 遍历输入数组,填充映射。
  13. for (int num : arr){
  14. // 如果数字在映射中不存在,它的频率是1。否则,在当前频率上加1。
  15. int currFrequency = counts.getOrDefault(num, 0) + 1;
  16. // 更新当前数字的频率。
  17. counts.put(num, currFrequency);
  18. // 如果当前数字的频率是目前为止最高的,存储它的频率以备后用。
  19. highestFrequency = Math.max(currFrequency, highestFrequency);
  20. }
  21. // 遍历数组中的唯一数字(请记住,Java中的Map不允许重复的键)。
  22. for (int key : counts.keySet()){
  23. // 如果当前数字具有最高的频率,则将其打印到控制台。
  24. if (counts.get(key) == highestFrequency){
  25. System.out.println(key);
  26. }
  27. }
  28. }

输出:

  1. 6
  2. 10
英文:

You would need a map (aka dictionary) data structure to solve this problem. This is the fastest and most concise solution I could come up with.

  1. public static void main(String[] args){
  2. int[] arr = new int[]{10, 5, 2, 1, 2, 4, 6, 6, 6, 6, 10, 10, 10};
  3. printMostFrequent(arr);
  4. }
  5. private static void printMostFrequent(int[] arr){
  6. // Key: number in input array
  7. // Value: amount of times that number appears in the input array
  8. Map&lt;Integer, Integer&gt; counts = new HashMap&lt;&gt;();
  9. // The most amount of times the same number appears in the input array. In this example, it&#39;s 4.
  10. int highestFrequency = 0;
  11. // Iterate through input array, populating map.
  12. for (int num : arr){
  13. // If number doesn&#39;t exist in map already, its frequency is 1. Otherwise, add 1 to its current frequency.
  14. int currFrequency = counts.getOrDefault(num, 0) + 1;
  15. // Update frequency of current number.
  16. counts.put(num, currFrequency);
  17. // If the current number has the highest frequency so far, store its frequency for later use.
  18. highestFrequency = Math.max(currFrequency, highestFrequency);
  19. }
  20. // Iterate through unique numbers in array (remember, a Map in Java allows no duplicate keys).
  21. for (int key : counts.keySet()){
  22. // If the current number has the highest frequency, then print it to console.
  23. if (counts.get(key) == highestFrequency){
  24. System.out.println(key);
  25. }
  26. }
  27. }

Output

  1. 6
  2. 10

huangapple
  • 本文由 发表于 2020年7月31日 10:25:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63184911.html
匿名

发表评论

匿名网友

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

确定