# 381. Insert Delete GetRandom O(1) - Duplicates allowed

![](/files/h9frWxaVcK19Tz8wA602)

![](/files/3Nh4iz6t2JWWgg4kRMBd)

![](/files/OEyQZUPirl6zdgnOPALS)

time: all O(1)

space: O(1\~n)

```java
class RandomizedCollection {
    
    private List<Integer> nums;
    private Map<Integer, Set<Integer>> map; // use set to store multi indexs
    private Random rand;
    
    public RandomizedCollection() {
        nums = new ArrayList<>();
        map = new HashMap<>();
        rand = new Random();
    }
    
    public boolean insert(int val) {
        boolean result = !map.containsKey(val); // this is tricky...
        
        map.computeIfAbsent(val, k -> new HashSet<>());
        map.get(val).add(nums.size()); // add (val, num.size())
        nums.add(val); // add nums in list
        return result;
    }
    
    public boolean remove(int val) {
        if (!map.containsKey(val)) {
            return false;
        }
        
        Set<Integer> deleteIndexs = map.get(val);
        
        // if remove middle position in muti indexs(sets)
        if (!deleteIndexs.contains(nums.size()-1)) { // lastnum put into dele...
            int deleteIndex = deleteIndexs.iterator().next(); // get one of indexs from sets to delete 
            int lastNum = nums.get(nums.size()-1); // the last number
            
            nums.set(deleteIndex, lastNum);
            
            map.get(lastNum).remove(nums.size()-1); // remove origin index
            map.get(lastNum).add(deleteIndex); // change to deleteIndex
            map.get(val).remove(deleteIndex); // remove delete val
        }
        
        // if remove last position
        map.get(val).remove(nums.size()-1);// remove map(val, last position index)
        if (map.get(val).size() == 0) { // if set is empty, directly remove 
            map.remove(val);
        }
        nums.remove(nums.size()-1); // remove last num in list
        return true;
    }
    
    public int getRandom() {
        return nums.get(rand.nextInt(nums.size()));
    }
}

/**
 * Your RandomizedCollection object will be instantiated and called as such:
 * RandomizedCollection obj = new RandomizedCollection();
 * boolean param_1 = obj.insert(val);
 * boolean param_2 = obj.remove(val);
 * int param_3 = obj.getRandom();
 */
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/design/381.-insert-delete-getrandom-o-1-duplicates-allowed.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
