hashmap:

Insert Delete Getrandom O1 Duplicates Allowed

Design a data structure that supports all following operations in average O(1) time. Note: Duplicate elements are allowed. insert(val): Inserts an item val to the collection. remove(val): Removes an item val from the collection if present. getRandom: Returns a random element from current collection of elements. The probability of each element being returned is linearly related to the number of same value the collection contains. Example // Init an empty collection.

by lek tin in "algorithm" "ood" "hashmap" access_time 2-min read