380. Insert Delete GetRandom O(1)

 

Implementing a Randomized Set in JavaScript

Introduction

In the world of data structures, efficiency is key. Today, I’m excited to share a solution for implementing a RandomizedSet in JavaScript. This data structure allows for efficient insertion, deletion, and random retrieval of elements, making it a versatile tool for various applications.

Problem Statement

The goal is to design a data structure that supports the following operations:

  1. insert(val): Inserts a value into the set. Returns true if the set did not already contain the specified element.

  2. remove(val): Removes a value from the set. Returns true if the set contained the specified element.

  3. getRandom(): Returns a random element from the set. Each element must have the same probability of being returned.

Approach

To achieve these functionalities efficiently, we'll utilize JavaScript's Set object, which offers constant time complexity for insertion and deletion. For random retrieval, converting the set to an array and using a random index is a straightforward approach.

1. Initialization:

  • Create an empty Set.

2. Insert:

  • Check if the value already exists in the set. If it does, return false. Otherwise, add the value to the set and return true.

3. Remove:

  • Check if the value exists in the set. If it doesn't, return false. Otherwise, delete the value from the set and return true.

4. Get Random:

  • Convert the set to an array and select a random index to return an element.

Code Implementation

Here's the JavaScript implementation for the RandomizedSet:


var RandomizedSet = function() {
    this.set = new Set();
};

/**
 * @param {number} val
 * @return {boolean}
 */
RandomizedSet.prototype.insert = function(val) {
    if(this.set.has(val)) {
        return false;
    }
    this.set.add(val);
    return true;
};

/**
 * @param {number} val
 * @return {boolean}
 */
RandomizedSet.prototype.remove = function(val) {
    if(!this.set.has(val)){
        return false;
    }
    this.set.delete(val);
    return true;
};

/**
 * @return {number}
 */
RandomizedSet.prototype.getRandom = function() {
    let arr = Array.from(this.set);
    const randomIndex = Math.floor(Math.random() * arr.length);
    return arr[randomIndex];
};

/**
 * Your RandomizedSet object will be instantiated and called as such:
 * var obj = new RandomizedSet()
 * var param_1 = obj.insert(val)
 * var param_2 = obj.remove(val)
 * var param_3 = obj.getRandom()
 */

Complexity Analysis

  • Time Complexity:

    • insert: O(1)O(1)

    • remove: O(1)O(1)

    • getRandom: O(1)O(1) on average, although converting the set to an array takes O(n)O(n)

  • Space Complexity: O(n)O(n)

    • The set uses space proportional to the number of elements stored.

Conclusion

The RandomizedSet efficiently supports insertion, deletion, and random retrieval of elements, making it a powerful tool for various applications. By leveraging JavaScript's Set object and array manipulation, we achieve these operations with optimal performance.

For more insights and detailed discussions, you can check out the related LeetCode solution: Insert Delete GetRandom O(1) - LeetCode.

Comments

Popular posts from this blog

3Sum Closest: O(n²) Two-Pointer Magic 🚀 leetcode: 16

Kadane's Algorithm: Maximum Subarray Problem - LeetCode(53) Solution

LeetCode Problem: Implement pow(x, n) leetcode:- 50