我要打印数组中重复次数最多的值。如果两个值的重复次数达到最大次数,则打印最大的一个值。我不知道如何打印最大的值。我尝试过此方法,它只是将打印出的重复值最大的值打印在数组中。
int[] a= { 3,2,3,2,2};
int count = 1, tempCount;
int repeated = a[0];
int temp = 0;
for (int i = 0; i < (a.length - 1); i++)
{
temp = a[i];
tempCount = 0;
for (int j = 1; j < a.length; j++)
{
if (temp == a[j])
tempCount++;
}
if (tempCount > count)
{
repeated = temp;
count = tempCount;
}
}
System.out.println(repeated);
如果假设数组元素为“ 3,2,3,3,2,4,5,4,6,4”,则必须打印4.(3号3次,4号3次)。 ....但是4是最大编号,因此输出为4)。现在如何更改我的代码?
答案 0 :(得分:3)
这里:
@material-ui/core/styles
您发现一个“新的”重复值,然后您无条件地分配了该值。
您需要区分两种情况:
repeated = temp;
解决了您的问题!
答案 1 :(得分:2)
将此代码中的j更改为等于0
for (int j = 1; j < a.length; j++)
,因为它跳过了数组的第一个元素,这导致3仅被计数两次。 如果较大的数字具有相等的计数,则此逻辑也应有所帮助
int[] a= {3,2,3,2,2, 2, 4, 4, 5 ,5};
int count = 1, tempCount;
int repeated = a[0];
int temp = 0;
for (int i = 0; i < (a.length - 1); i++)
{
temp = a[i];
tempCount = 0;
for (int j = 0; j < a.length; j++)
{
if (temp == a[j])
tempCount++;
}
if (tempCount ==count )
{
if(temp>repeated ){
repeated = temp;
count = tempCount;
}
}
if (tempCount > count)
{
repeated = temp;
count = tempCount;
}
}
System.out.println(repeated);
}
}
编辑,我知道它很懒,但是我将其保留为海报代码格式。
答案 2 :(得分:1)
我可能不会只遍历数组多次,而只遍历一次并计算发生的次数。
在这里Map<Integer, Integer>
会有所帮助,特别是如果数字可能为负或有“空洞”(即您有[1,2,5,9,1,9,9]之类的东西)。在这里,键将是数字,值将是计数。示例:
Map<Integer,Integer> counts = new HashMap<>();
for(int n : a ) {
counts.merge(n, 1, (value,increment) -> value + increment );
}
下一步,您可以按计数对条目进行排序,然后取最高记录,或者再次进行迭代,并跟踪条目(如果它们的计数高于当前最大值,并且其键高于当前键)。
答案 3 :(得分:0)
这种方法在您的问题下方的评论中被称为 overkill ,因为其他人(但我)建议使用地图。我认为这是一种有效的方法,因为使用了数据结构Map
。查看代码中的注释:
public static void main(String[] args) {
int[] a = { 3, 2, 3, 2, 2 };
int[] b = { 3, 2, 3, 3, 2, 4, 5, 4, 6, 4 };
// create a data structure that holds the element and its count
Map<Integer, Integer> occurrences = new HashMap<Integer, Integer>();
/*
* go through the array and store each element found as the key and how often it
* was found as the value
*/
for (int n : b) {
if (occurrences.containsKey(n)) {
/*
* if the key is already contained, increment the value (additional occurrence
* found)
*/
occurrences.put(n, occurrences.get(n) + 1);
} else {
// otherwiese just add the key with value one (first occurrence)
occurrences.put(n, 1);
}
}
// now you have all the elements with its occurrences, go find the one to be displayed
// print them once (not necessary for the result)
occurrences.forEach((key, value) -> System.out.println(key + " : " + value));
// get the largest number with the largest occurrence count
int maxValue = 0;
int maxKey = 0;
for (int i : occurrences.keySet()) {
if (occurrences.get(i) > maxValue) {
// if you find a larger value, set the current key as max key
maxKey = i;
maxValue = occurrences.get(i);
} else if (occurrences.get(i) == maxValue) {
/*
* if you find a value equal to the current largest one, compare the keys and
* set/leave the larger one as max key
*/
if (i > maxKey) {
maxKey = i;
}
} else {
// no need for handling a smaller key found
continue;
}
}
System.out.println("Largest key with largest value is " + maxKey);
}
答案 4 :(得分:-1)
这是您的问题的解决方案。阅读代码中的注释以获得更多理解。您的代码的复杂度为O(n ^ 2)。下面的代码具有O(n)复杂度,并且速度更快。
public static void main(String[] args){
int[] arr = new int[]{3,2,3,3,2,4,5,4,6,4};
// Assuming A[i] <= 100. Using freq[] to capture the frequency of number
// freq[] will behave as a Map where index will be the number and freq[index] will be the frequency
// of that number
int[] freq = new int[101];
for(int i=0;i<arr.length;i++){
int num = arr[i];
freq[num]++;
}
int max = 0;
int ans = 0;
for(int i=0;i<freq.length;i++){
if(freq[i] >= max){
ans = i;
max = freq[i];
}
}
System.out.println(ans);
}