DEV Community

Discussion on: Data Structures: Bidirectional Map

Collapse
 
jishen73 profile image
John Shen

Thanks for the writeup. This is intriguing as I've had moments where I would've liked to use something similar where I'd look up the key by giving a value. In this Bidirectional Map, are the keys and values required to be unique? If we have two keys with the same value, would this not cause a problem if we tried to look up the keys by the value?

Collapse
 
pretaporter profile image
Maksim • Edited

Yeah, indeed keys and values required to be unique.

if (this.hasKey(key) || this.hasValue(value)) {
  throw new Error('Key or Value already exists');
}
Enter fullscreen mode Exit fullscreen mode

As you can see in current implementation, set method will throw an error in case if map already consists key or value.