开发者社区> 问答> 正文

应该怎么用map找出次数最多的元素

After celebrating the midcourse the students of one of the faculties of the Berland State University decided to conduct a vote for the best photo. They published the photos in the social network and agreed on the rules to choose a winner: the photo which gets most likes wins. If multiple photoes get most likes, the winner is the photo that gets this number first.
Help guys determine the winner photo by the records of likes.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the total likes to the published photoes.
The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 1 000 000), where ai is the identifier of the photo which got the i-th like.
Output
Print the identifier of the photo which won the elections.
Examples
input
5
1 3 2 2 1
output
2
input
9
100 200 300 200 100 300 300 100 200
output
300

展开
收起
a123456678 2016-03-09 11:58:49 2523 0
1 条回答
写回答
取消 提交回答
  • 伪代码表示一下:
    int maxCount = 0, maxKey = -1;
    for entry in map {

    if (entry.count > maxCount) {
        maxCount = entry.count;
        maxKey = entry.key;
    }

    }
    return maxKey;

    2019-07-17 18:55:47
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载