算法查找出现的最多的一排数字 - C ++ [英] Algorithm for finding the number which appears the most in a row - C++

查看:134
本文介绍了算法查找出现的最多的一排数字 - C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做一个算法求解一个问题帮助:有一排与该行中出现不同的时间数字,我需要找到出现的数量最多,有多少次是该行中,例如:

I need a help in making an algorithm for solving one problem: There is a row with numbers which appear different times in the row, and i need to find the number that appears the most and how many times it's in the row, ex:

1-1-5-1-3-7-2-1-8-9-1-2

1-1-5-1-3-7-2-1-8-9-1-2

这将是1和它出现5次。

That would be 1 and it appears 5 times.

该算法要快(这是我的问题)。 任何想法?

The algorithm should be fast (that's my problem). Any ideas ?

推荐答案

您可以保持哈希表并存储每个元素的计数在该结构中,像这样

You could keep hash table and store a count of every element in that structure, like this

h[1] = 5
h[5] = 1
...

这篇关于算法查找出现的最多的一排数字 - C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆