#Insert into HashMap if not duplicate value [solved c:]

4 messages · Page 1 of 1 (latest)

rancid kiln
#

what is the best way to insert a key value pair into a hashmap iff the value is not already in the map under any key
one solution i have thought of is rust let (key, value) = ...; if hashmap.values().all(|&x| x != value) { hashmap.insert(key, value); } is there a better way?

zenith prairie
#

Best way would probably to keep the values in a hashset. If there's multiple of some values, then a hashmap of values to count. If you need to find the key for each value, then a hashmap from values to keys, aka bidirectional map.

If this is the only thing you're doing, then just swap the keys and values.

rancid kiln
#

thanku