某个值出现在hastable中的次数 [英] Number of times a certain value appear in a hastable

查看:81
本文介绍了某个值出现在hastable中的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,我有一个哈希表,其中包含大约1000000个条目,并且我需要查找某个值在哈希表中出现多少次.

My problem is, that i have a hash table which has about 1000000 entries and i need to find how many times a certain value appears in the hash table.

key1-->val1
key2-->val2
key3-->val3
key4-->val1
key5-->val1

output:
val1==3
val2==1
val3==1

我可以在不同的键下存储相同的值吗?如果可能的话,如何有效地找到出现次数?

Can I store same value under different keys?And if possible how can I find the number of appearances efficiently?

推荐答案

是的,您可以将相同的值存储在不同的键下.为了计算发生次数,我可能会维护一个辅助映射,该映射将值映射到计数.每次在第一个映射中插入一个值时,都要检查它是否在第二个映射中有一个条目.如果是这样,您可以简单地增加计数.否则,您将插入一个计数为1的新条目.

Yes, you can store the same value under different keys. To count the occurrences, I would perhaps maintain a secondary map that maps the value to a count. Every time you insert a value into the first map, you check to see if it has an entry in the second map. If it does, you can simply increment the count. Otherwise, you insert a new entry with a count of 1.

这篇关于某个值出现在hastable中的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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