Java 打印最大重复值
我想打印数组中重复次数最多的值。如果两个值重复的次数最多,则打印最大的值。我不知道如何打印最大的值。我尝试了这个方法。它只打印数组中重复次数最多的值Java 打印最大重复值,java,Java,我想打印数组中重复次数最多的值。如果两个值重复的次数最多,则打印最大的值。我不知道如何打印最大的值。我尝试了这个方法。它只打印数组中重复次数最多的值 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]; tempCoun
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);
int[]a={3,2,3,2,2};
int count=1,tempCount;
int repeated=a[0];
内部温度=0;
对于(int i=0;i<(a.length-1);i++)
{
温度=a[i];
tempCount=0;
对于(int j=1;j计数)
{
重复=温度;
计数=临时计数;
}
}
系统输出打印项次(重复);
如果假设数组元素为“3,2,3,3,2,4,5,4,6,4”,则它必须打印4。(3号三次,4号三次…..但4是最大的数字,因此输出为4)。现在如何更改代码?此处:
repeated = temp;
您发现了一个“新”的重复值,并无条件地分配该值
您需要区分两种情况:
if (tempCount == count && temp > repeated)
{
// updates for EQUAL count, but only for larger numbers
repeated = temp;
count = tempCount;
}
if (tempCount > count)
{
// updates for larger count, small/large value doesn't matter
repeated = temp;
count = tempCount;
}
解决你的问题 将此代码中的j更改为等于0
for (int j = 1; j < a.length; j++)
for(int j=1;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);
}
}
int[]a={3,2,3,2,2,4,4,5};
int count=1,tempCount;
int repeated=a[0];
内部温度=0;
对于(int i=0;i<(a.length-1);i++)
{
温度=a[i];
tempCount=0;
对于(int j=0;j重复){
重复=温度;
计数=临时计数;
}
}
如果(临时计数>计数)
{
重复=温度;
计数=临时计数;
}
}
系统输出打印项次(重复);
}
}
编辑我知道它很懒,但我把它保存在海报代码格式中。我可能只迭代一次,然后计算发生的次数,而不是在数组上迭代多次
Map
在这里会有所帮助,尤其是当数字可能为负数或有“洞”(即,你有类似于[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 );
}
Map counts=newhashmap();
对于(int n:a){
合并(n,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);
}
publicstaticvoidmain(字符串[]args){
int[]arr=新的int[]{3,2,3,3,2,4,5,4,6,4};
//假设[i]这是一种在您问题下方的注释中被称为“过度杀戮”的方法,因为其他人(除了我)建议使用映射。我认为这是一种有效的方法,因为使用了数据结构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);
}
publicstaticvoidmain(字符串[]args){
int[]a={3,2,3,2,2};
int[]b={3,2,3,3,2,4,5,4,6,4};
//创建包含元素及其计数的数据结构
映射出现次数=新建HashMap();
/*
*遍历数组并将找到的每个元素作为键存储,以及存储的频率
*找到的值为
*/
对于(int n:b){
如果(事件。容器(n)){
/*
*如果已包含该键,则增加该值(额外出现
*发现)
*/
事件。放置(n,事件。获取(n)+1);
}否则{
//其他Wiese只需添加值为1的键(第一次出现)
事件。put(n,1);
}
}
//现在您已经拥有了所有元素及其引用,请查找要显示的元素
//打印一次(结果不需要)
forEach((key,value)->System.out.println(key+“:”+value));
//获取出现次数最大的最大数字
int maxValue=0;
int maxKey=0;
对于(int i:executions.keySet()){
if(出现次数.获取(i)>最大值){
//如果找到较大的值,请将当前关键点设置为“最大关键点”
maxKey=i;
maxValue=出现次数。获取(i);
}else if(executions.get(i)=maxValue){
/*
*如果找到与当前最大值相等的值,请比较键和
*将较大的设置/保留为最大关键点
*/
如果(i>maxKey){
maxKey=i;
}
}否则{
//不需要处理较小的密钥
继续;
}
}
System.out.println(“最大值的最大键为”+maxKey);
}
Just change count=0它正在按预期工作。使用{3,2,3,2,2,4,4}测试此修复程序
,当它应该打印4
时,它会打印2
。这是一个不同的问题。我们需要添加一个检查,看看新的temp是否大于当前最大的数,这是他的问题,而这个答案不能解决它?这应该有助于用int[]a={3,2,3,2,2,2,4,5,5}测试它;
在这种情况下似乎不起作用。打印5
,期望2
。为未来的读者提供更强大的(抽象)解决方案是件好事。尽管OP可能只是在寻找他当前算法中的问题。但我投票支持给出映射的解决方案!