Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Hash have duplicate keys or values

Tags:

hash

perl

Can a Hash have duplicate keys or values?

like image 408
Steffan Harris Avatar asked May 25 '11 02:05

Steffan Harris


People also ask

Are duplicates allowed in hash table?

This question is going to depend heavily on the hash table itself, since both are possible. For example, std::unordered_map doesn't allow duplicates, while std::unordered_multimap does allow duplicates.

Can a hash map have duplicate values?

Duplicates: HashSet doesn't allow duplicate values. HashMap stores key, value pairs and it does not allow duplicate keys.

Does hash have a key?

A hash table stores key and value pairs in a list that is accessible through its index. Because key and value pairs are unlimited, the hash function will map the keys to the table size. A hash value then becomes the index for a specific element.

Can a hash have duplicate keys Ruby?

Short answer is no, hashes need to have unique keys.


1 Answers

Please try and run this code, it executes without errors. I hope this is what you were asking!

#!/usr/bin/perl

use strict;
use warnings;

my %hash = ('a' => 1, 'a' => 2, 'b' => 4 );

print values %hash, "\n\n";
print keys %hash, "\n\n";
like image 129
Mayank Avatar answered Oct 22 '22 00:10

Mayank