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

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

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

问题

以下是翻译好的内容:

我需要打印出数组中出现次数最多的数字假设我有一个如下的 ```int``` 数组

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

然后我需要打印出 3因为它是出现次数最多的元素但是我还需要打印出作为频繁出现的元素假设我现在有这个数组

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

然后我需要打印出有序的35 和 7因为这三个元素一起是出现次数最多的因此输出需要像这样

    3
    5
    7

这是我目前的代码它可以打印出出现次数最多的一个数字):

    Arrays.sort(diceSumArray);
        
    int maxCount = 1;
    int index = 1;

    int r = diceSumArray[0];

    for (int i = 1; i < diceSumArray.length; i++) {
        if (diceSumArray[i] == diceSumArray[i-1]) {
            index++;
        } else {
            if (index > maxCount) {
                maxCount = index;
                r = diceSumArray[i-1];
            }
            index = 1;
        }
    }
   
    // 不确定在哪里放打印语句,或者下面的代码是否必要
    System.out.println(r);

    if (index > maxCount) {
        maxCount = index;
        r = diceSumArray[diceSumArray.length -1];
    }
英文:

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:

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:

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:

3
5
7

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

   Arrays.sort(diceSumArray);
    
    int maxCount = 1;
    int index = 1;

    int r = diceSumArray[0];

    for (int i = 1; i &lt; diceSumArray.length; i++) {
        if (diceSumArray[i] == diceSumArray[i-1]) {
            index++;
        } else {
            if (index &gt; maxCount) {
                maxCount = index;
                r = diceSumArray[i-1];
            }
            index = 1;
        }
    }
   
    // not sure where to put the print or if the code below is necessary 
    System.out.println(r);

    if (index &gt; maxCount) {
        maxCount = index;
        r = diceSumArray[diceSumArray.length -1];
    }

答案1

得分: 1

以下是翻译好的内容:

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

private static List<Integer> getMostFreq(int[] arr){
    List<Integer> list = new ArrayList<>();
    if(arr == null || arr.length == 0)
        return list;
    HashMap<Integer, Integer> map = new HashMap<>();
    for(int num : arr)
        if(!map.containsKey(num))
            map.put(num,1);
        else
            map.replace(num, map.get(num)+1);
    int max = Integer.MIN_VALUE;
    for(HashMap.Entry<Integer,Integer> item : map.entrySet())
        if(item.getValue() > max)
            max = item.getValue();
    for(HashMap.Entry<Integer,Integer> item : map.entrySet())
        if(item.getValue() == max)
            list.add(item.getKey());
    return list;
}
英文:

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:

private static List&lt;Integer&gt; getMostFreq(int[] arr){
		List&lt;Integer&gt; list = new ArrayList&lt;&gt;();
		if(arr == null || arr.length == 0)
			return list;
		HashMap&lt;Integer, Integer&gt; map = new HashMap&lt;&gt;();
		for(int num : arr)
			if(!map.containsKey(num))
				map.put(num,1);
			else
				map.replace(num, map.get(num)+1);
		int max = Integer.MIN_VALUE;
		for(HashMap.Entry&lt;Integer,Integer&gt; item : map.entrySet())
			if(item.getValue() &gt; max)
				max = item.getValue();
		for(HashMap.Entry&lt;Integer,Integer&gt; item : map.entrySet())
			if(item.getValue() == max)
				list.add(item.getKey());
		return list;
}

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:

确定