Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用Hashmaps的数组中的最大频率数_C++_Arrays_Data Structures_Hashmap - Fatal编程技术网

C++ 使用Hashmaps的数组中的最大频率数

C++ 使用Hashmaps的数组中的最大频率数,c++,arrays,data-structures,hashmap,C++,Arrays,Data Structures,Hashmap,问题: 您将得到一个整数数组,其中包含随机顺序的数字。编写一个程序,查找并返回给定输入中出现最大次数的数字 如果两个或多个元素争夺最大频率,则返回数组中首先出现的元素 输入格式: 第1行:整数N,即数组的大小 第2行:N个整数,它们是数组的元素,由空格分隔 输出格式: 最频繁元素 限制条件: 0(最大频率){ maxFreq=map[input[i]]; } } map[input[i]]=1; } 对于(int i=0;i

问题:

您将得到一个整数数组,其中包含随机顺序的数字。编写一个程序,查找并返回给定输入中出现最大次数的数字

如果两个或多个元素争夺最大频率,则返回数组中首先出现的元素

输入格式:

第1行:整数N,即数组的大小
第2行:N个整数,它们是数组的元素,由空格分隔

输出格式:

最频繁元素

限制条件:

0(最大频率){ maxFreq=map[input[i]]; } } map[input[i]]=1; } 对于(int i=0;i我认为这是计算元素频率的有效方法。 无序地图mp

// Traverse through array elements and 
// count frequencies 
for (int i = 0; i < n; i++) 
    mp[arr[i]]++; 

// Traverse through map and print frequencies 
for (auto x : mp) 
    cout << x.first << " " << x.second << endl; 
// found the most frequent item.
 int max_count = 0, res = -1; 
for (auto i : mp) { 
    if (max_count < i.second) { 
        res = i.first; 
        max_count = i.second; 
    } 
} 
//遍历数组元素和
//计数频率
对于(int i=0;i
public static int maxFrequencyNumber(int[] arr){ 
    if(arr.length == 0)
        return -1;
    int maxFreq = 0;
    int number = -1;
    HashMap<Integer,Integer> map = new HashMap<>();
    
    for(int i=0;i<arr.length;i++)
    {
        if(map.containsKey(arr[i]))
        {
            map.put(arr[i],map.get(arr[i])+1);
        }
        else {
            map.put(arr[i], 1);
        }
    }
    // using set data structure
    Set<Integer> keySet = map.keySet();
    for(Integer i:keySet)
    {
        if(map.get(i) > maxFreq)
        {
            number = i;
            maxFreq = map.get(i);
        }
    }
    return number;
}
公共静态int-maxFrequencyNumber(int[]arr){
如果(arr.length==0)
返回-1;
int maxFreq=0;
整数=-1;
HashMap=newHashMap();
对于(int i=0;i maxFreq)
{
数字=i;
maxFreq=map.get(i);
}
}
返回号码;
}

}

您应该从函数返回值,而不是打印它。(您需要同时跟踪值(或索引)和频率。)是我还是您忘记在
map[inpuit[i]]=1
之前添加
else
-语句?此外,整个
if(map.count(input[i])>0
内容也不需要<代码>映射[输入[i]]++只会在没有值的情况下工作。虽然它输出所有元素,但不只是最频繁的…@melk我已经做了必要的更改。
#include <unordered_map>
using namespace std;


int highestFrequency(int *input, int n){
    unordered_map<int, int> map;
    int maxFreq = 0;
    for(int i = 0; i < n; i++){
        if(map.count(input[i]) > 0){
            map[input[i]]++;
            if(map[input[i]] > maxFreq){
                maxFreq = map[input[i]];
            }
        }
        map[input[i]] = 1;
    }
    for(int i = 0; i < n; i++){
        if(map[input[i]] == maxFreq){
            cout << input[i];
        }
    }

    /* Don't write main().
     * the input array is already passed as function argument.
     * Taking input and printing output is handled automatically.
     */ 
}
// Traverse through array elements and 
// count frequencies 
for (int i = 0; i < n; i++) 
    mp[arr[i]]++; 

// Traverse through map and print frequencies 
for (auto x : mp) 
    cout << x.first << " " << x.second << endl; 
// found the most frequent item.
 int max_count = 0, res = -1; 
for (auto i : mp) { 
    if (max_count < i.second) { 
        res = i.first; 
        max_count = i.second; 
    } 
} 
public static int maxFrequencyNumber(int[] arr){ 
    if(arr.length == 0)
        return -1;
    int maxFreq = 0;
    int number = -1;
    HashMap<Integer,Integer> map = new HashMap<>();
    
    for(int i=0;i<arr.length;i++)
    {
        if(map.containsKey(arr[i]))
        {
            map.put(arr[i],map.get(arr[i])+1);
        }
        else {
            map.put(arr[i], 1);
        }
    }
    // using set data structure
    Set<Integer> keySet = map.keySet();
    for(Integer i:keySet)
    {
        if(map.get(i) > maxFreq)
        {
            number = i;
            maxFreq = map.get(i);
        }
    }
    return number;
}