Reputation: 376
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?
Upvotes: 0
Views: 809
Reputation: 10900
Yes, a hashtable can store the same value under different keys. In Java, you can call the Hashtable#values method to get all the values within that hashtable. You can then use it to count how many times a certain value occurs.
Given the big size of the hashtable (1 million entries), I would search for a more efficient solution (like the one proposed by @Vivin Paliath).
Upvotes: 1
Reputation: 95518
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
.
Upvotes: 3