例如:
int a = { 2, 456, 34, 3456, 2, 435, 2, 456, 2}
输出结果为4,因为数字2出现了4次。这是数字2最大的出现次数。
int a = { 2, 456, 34, 3456, 2, 435, 2, 456, 2}
输出结果为4,因为数字2出现了4次。这是数字2最大的出现次数。
将数组排序,然后快速遍历以计算每个数字的数量。该算法的时间复杂度为O(N*logN)。
或者可以创建一个哈希表,使用数字作为键。在哈希表中存储每个元素对应的计数器。您可以在一次遍历中计算所有元素的数量;然而,该算法的复杂度现在取决于哈希函数的复杂度。
针对空间优化:
快速排序(例如)然后迭代所有项,仅跟踪最大计数。 最佳情况为O(N log N)。
针对速度优化:
迭代所有元素,跟踪不同计数。 该算法将始终为O(n)。
一个可能利用STL的C++实现如下:
#include <iostream>
#include <algorithm>
#include <map>
// functor
struct maxoccur
{
int _M_val;
int _M_rep;
maxoccur()
: _M_val(0),
_M_rep(0)
{}
void operator()(const std::pair<int,int> &e)
{
std::cout << "pair: " << e.first << " " << e.second << std::endl;
if ( _M_rep < e.second ) {
_M_val = e.first;
_M_rep = e.second;
}
}
};
int
main(int argc, char *argv[])
{
int a[] = {2,456,34,3456,2,435,2,456,2};
std::map<int,int> m;
// load the map
for(unsigned int i=0; i< sizeof(a)/sizeof(a[0]); i++)
m [a[i]]++;
// find the max occurence...
maxoccur ret = std::for_each(m.begin(), m.end(), maxoccur());
std::cout << "value:" << ret._M_val << " max repetition:" << ret._M_rep << std::endl;
return 0;
}
一点伪代码:
//split string into array firts
strsplit(numbers) //PHP function name to split a string into it's components
i=0
while( i < count(array))
{
if(isset(list[array[i]]))
{
list[array[i]]['count'] = list + 1
}
else
{
list[i]['count'] = 1
list[i]['number']
}
i=i+1
}
usort(list) //usort is a php function that sorts an array by its value not its key, Im assuming that you have something in c++ that does this
print list[0]['number'] //Should contain the most used number
现在,到了2022年,我们有以下新特性:
std::unordered_map
using
语句std::ranges
库有了这些,我们现在可以写出:
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
namespace rng = std::ranges;
int main() {
// Demo data
std::vector data{ 2, 456, 34, 3456, 2, 435, 2, 456, 2 };
// Count values
using Counter = std::unordered_map<decltype (data)::value_type, std::size_t> ;
Counter counter{}; for (const auto& d : data) counter[d]++;
// Get max
const auto& [value, count] = *rng::max_element(counter, {}, &Counter::value_type::second);
// Show output
std::cout << '\n' << value << " found " << count << " times\n";
}
这将是O(n)的时间复杂度............但问题是大量的数组可以使用相同大小的另一个数组............
for(i=0;i
mar=count[o]; index=o;
for(i=0;i
然后输出将是.........元素index在该数组中出现了max次........
这里a[]是数据数组,我们需要在其中搜索某个数字的最大出现次数......
count[]包含每个元素的计数.......... 注意:我们已经知道数据范围将在数组中。 例如,该数组中的数据范围从1到100.......然后有一个100个元素的计数数组来跟踪,如果它出现,则将索引值增加一........
std::tr1::unordered_map
。unordered_map
中插入/更新计数,然后进行partial_sort_copy
,这是O(n)的操作。2*O(n) ~= O(n)。#include <unordered_map>
#include <vector>
#include <algorithm>
#include <iostream>
namespace {
// Only used in most_frequent but can't be a local class because of the member template
struct second_greater {
// Need to compare two (slightly) different types of pairs
template <typename PairA, typename PairB>
bool operator() (const PairA& a, const PairB& b) const
{ return a.second > b.second; }
};
}
template <typename Iter>
std::pair<typename std::iterator_traits<Iter>::value_type, unsigned int>
most_frequent(Iter begin, Iter end)
{
typedef typename std::iterator_traits<Iter>::value_type value_type;
typedef std::pair<value_type, unsigned int> result_type;
std::tr1::unordered_map<value_type, unsigned int> counts;
for(; begin != end; ++begin)
// This is safe because new entries in the map are defined to be initialized to 0 for
// built-in numeric types - no need to initialize them first
++ counts[*begin];
// Only need the top one at this point (could easily expand to top-n)
std::vector<result_type> top(1);
std::partial_sort_copy(counts.begin(), counts.end(),
top.begin(), top.end(), second_greater());
return top.front();
}
int main(int argc, char* argv[])
{
int a[] = { 2, 456, 34, 3456, 2, 435, 2, 456, 2 };
std::pair<int, unsigned int> m = most_frequent(a, a + (sizeof(a) / sizeof(a[0])));
std::cout << "most common = " << m.first << " (" << m.second << " instances)" << std::endl;
assert(m.first == 2);
assert(m.second == 4);
return 0;
}