英文:
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}
然后我需要打印出(有序的)3、5 和 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 < diceSumArray.length; i++) {
if (diceSumArray[i] == diceSumArray[i-1]) {
index++;
} else {
if (index > 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 > 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<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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论