The Wayback Machine - https://web.archive.org/web/20240913040746/https://www.geeksforgeeks.org/design-a-data-structure-that-supports-insert-delete-search-and-getrandom-in-constant-time/
Open In App

Design a data structure that supports insert, delete, search and getRandom in constant time

Last Updated : 19 Apr, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Solve Problem
Easy
83.06%
1.4K

Design a data structure that supports the following operations in O(1) time.

  • insert(x): Inserts an item x to the data structure if not already present.
  • remove(x): Removes item x from the data structure if present. 
  • search(x): Searches an item x in the data structure.
  • getRandom(): Returns a random element from the current set of elements 

We can use hashing to support the first 3 operations in O(1) time. How to do the 4th operation? The idea is to use a resizable array (ArrayList in Java, vector in C) together with hashing. Resizable arrays support insert in O(1) amortized time complexity. To implement getRandom(), we can pick a random number from 0 to size-1 (size is the number of current elements) and return the element at that index. The hash map stores array values as keys and array indexes as values.

Following are detailed operations.

  • insert(x) 
    1. Check if x is already present by doing a hash map lookup. 
    2. If not present, then insert it at the end of the array. 
    3. In the hash table, x is added as the key, and the last array index as the index.
  • remove(x) 
    1. Check if x is present by doing a hash map lookup. 
    2. If present, then find its index and remove it from a hash map. 
    3. Swap the last element with this element in an array and remove the last element. 
      Swapping is done because the last element can be removed in O(1) time. 
    4. Update index of the last element in a hash map.
  • getRandom() 
    1. Generate a random number from 0 to last index. 
    2. Return the array element at the randomly generated index.
  • search(x) 
    • Do a lookup for x in hash map.

Below is the implementation of the data structure:

C++
#include <bits/stdc++.h>
using namespace std;

class myStructure {
    vector<int> arr;
    unordered_map<int, int> Map;

public:
    void insert(int x)
    {
        if (Map.find(x) != Map.end())
            return;
        int index = arr.size();
        arr.push_back(x);
        Map.insert({ x, index });
    }

    void remove(int x)
    {
        if (Map.find(x) == Map.end())
            return;
        int index = Map.at(x);
        Map.erase(x);
        if (index != arr.size() - 1) {
            int last = arr.size() - 1;
            swap(arr[index], arr[last]);
            if (last != index) {
                Map[arr[index]] = index;
            }
        }
        arr.pop_back();
    }

    int search(int x)
    {
        if (Map.find(x) != Map.end())
            return Map.at(x);
        return -1;
    }

    int getRandom()
    {
        srand(time(NULL));
        int random_index = rand() % arr.size();
        return arr[random_index];
    }
};

int main()
{
    myStructure ds;
    ds.insert(10);
    ds.insert(20);
    ds.insert(30);
    ds.insert(40);
    cout << ds.search(30) << endl;
    ds.remove(40);
    ds.insert(50);
    cout << ds.search(50) << endl;
    cout << ds.getRandom() << endl;
    return 0;
}

// This code is modified by Susobhan Akhuli
Java
import java.util.*;

class MyStructure {
    List<Integer> arr = new ArrayList<>();
    Map<Integer, Integer> map = new HashMap<>();

    public void insert(int x)
    {
        if (!map.containsKey(x)) {
            int index = arr.size();
            arr.add(x);
            map.put(x, index);
        }
    }

    public void remove(int x)
    {
        if (map.containsKey(x)) {
            int index = map.get(x);
            map.remove(x);
            if (index != arr.size() - 1) {
                int last = arr.size() - 1;
                Collections.swap(arr, index, last);
                if (last != index) {
                    map.put(arr.get(index), index);
                }
            }
            arr.remove(arr.size() - 1);
        }
    }

    public int search(int x)
    {
        return map.getOrDefault(x, -1);
    }

    public int getRandom()
    {
        Random rand = new Random();
        int randomIndex = rand.nextInt(arr.size());
        return arr.get(randomIndex);
    }
}

public class Main {
    public static void main(String[] args)
    {
        MyStructure ds = new MyStructure();
        ds.insert(10);
        ds.insert(20);
        ds.insert(30);
        ds.insert(40);
        System.out.println(ds.search(30));
        ds.remove(40);
        ds.insert(50);
        System.out.println(ds.search(50));
        System.out.println(ds.getRandom());
    }
}

// This code is modified by Susobhan Akhuli
Python3
import random

class MyStructure:
    def __init__(self):
        self.arr = []
        self.map = {}
        self.size=0

    def insert(self, x):
        if x not in self.map:
            index = self.size
            self.arr.append(x)
            self.size+=1
            self.map[x] = index

    def remove(self, x):
        if x in self.map:
            index = self.map[x]
            del self.map[x]
            if index != self.size - 1:
                last = self.size - 1
                self.arr[index], self.arr[last] = self.arr[last], self.arr[index]
                if last != index:
                    self.map[self.arr[index]] = index
            self.arr.pop()
            self.size-=1

    def search(self, x):
        return self.map.get(x, -1)

    def getRandom(self):
        random_index = random.randint(0, self.size - 1)
        return self.arr[random_index]

ds = MyStructure()
ds.insert(10)
ds.insert(20)
ds.insert(30)
ds.insert(40)
print(ds.search(30))
ds.remove(40)
ds.insert(50)
print(ds.search(50))
print(ds.getRandom())

# This code is modified by Susobhan Akhuli
C#
using System;
using System.Collections.Generic;

class MyStructure {
    List<int> arr = new List<int>();
    Dictionary<int, int> map = new Dictionary<int, int>();

    public void insert(int x)
    {
        if (!map.ContainsKey(x)) {
            int index = arr.Count;
            arr.Add(x);
            map.Add(x, index);
        }
    }

    public void Remove(int x)
    {
        if (map.ContainsKey(x)) {
            int index = map[x];
            map.Remove(x);
            if (index != arr.Count - 1) {
                int last = arr.Count - 1;
                arr[index] = arr[last];
                if (last != index) {
                    map[arr[index]] = index;
                }
            }
            arr.RemoveAt(arr.Count - 1);
        }
    }

    public int Search(int x)
    {
        return map.ContainsKey(x) ? map[x] : -1;
    }

    public int GetRandom()
    {
        Random rand = new Random();
        int randomIndex = rand.Next(arr.Count);
        return arr[randomIndex];
    }
}

class MainClass {
    public static void Main(string[] args)
    {
        MyStructure ds = new MyStructure();
        ds.insert(10);
        ds.insert(20);
        ds.insert(30);
        ds.insert(40);
        Console.WriteLine(ds.Search(30));
        ds.Remove(40);
        ds.insert(50);
        Console.WriteLine(ds.Search(50));
        Console.WriteLine(ds.GetRandom());
    }
}

// This code is modified by Susobhan Akhuli
JavaScript
class MyStructure {
    constructor() {
        this.arr = [];
        this.map = new Map();
    }

    insert(x) {
        if (!this.map.has(x)) {
            let index = this.arr.length;
            this.arr.push(x);
            this.map.set(x, index);
        }
    }

    remove(x) {
        if (this.map.has(x)) {
            let index = this.map.get(x);
            this.map.delete(x);
            if (index !== this.arr.length - 1) {
                let last = this.arr.length - 1;
                this.arr[index] = this.arr[last];
                if (last != index) {
                    this.map.set(this.arr[index], index);
                }
            }
            this.arr.pop();
        }
    }

    search(x) {
        return this.map.has(x) ? this.map.get(x) : -1;
    }

    getRandom() {
        let randomIndex = Math.floor(Math.random() * this.arr.length);
        return this.arr[randomIndex];
    }
}

let ds = new MyStructure();
ds.insert(10);
ds.insert(20);
ds.insert(30);
ds.insert(40);
console.log(ds.search(30));
ds.remove(40);
ds.insert(50);
console.log(ds.search(50));
console.log(ds.getRandom());

// This code is modified by Susobhan Akhuli

Output
2
3
50

Time Complexity: O(1) for all operations.
Auxiliary Space: O(n) for storing the elements in the data structure.



Previous Article
Next Article

Similar Reads

Design a data structure that supports insert, delete, getRandom in O(1) with duplicates
Design a Data Structure that can support the following operations in O(1) Time Complexity. insert(x): Inserts x in the data structure. Returns True if x was not present and False if it was already present.remove(x): Removes x from the data structure, if present.getRandom(): Returns any value present in the stream randomly. The probability of each e
9 min read
Design a structure which supports insertion and first non-repeating element in O(1) time
Design a Data structure which supports insertion and first non-repeating element in O(1) time. Operations that are supported by the data structure: Insertion: Insert a element into the data structure.First non-repeating Element: First non-repeating element into the array. Note: If there is no non-repeating element in the array then print -1. Consid
12 min read
Design a dynamic stack using arrays that supports getMin() in O(1) time and O(1) extra space
Design a special dynamic Stack using an array that supports all the stack operations such as push(), pop(), peek(), isEmpty(), and getMin() operations in constant Time and Space complexities. Examples: Assuming the right to left orientation as the top to bottom orientation and performing the operations: Push(10): 10 is added to the top of the stack
15+ min read
Design a stack that supports getMin() in O(1) time and O(1) extra space
Design a Data Structure SpecialStack that supports all the stack operations like push(), pop(), isEmpty(), isFull() and an additional operation getMin() which should return minimum element from the SpecialStack. All these operations of SpecialStack must have a time and space complexity of O(1). Note: To implement SpecialStack, you should only use s
15+ min read
Trie Data Structure | Insert and Search
The Trie data structure is a tree-like data structure used for storing a dynamic set of strings. It is commonly used for efficient retrieval and storage of keys in a large dataset. The structure supports operations such as insertion, search, and deletion of keys, making it a valuable tool in fields like computer science and information retrieval. I
15+ min read
Implementation of Search, Insert and Delete in Treap
We strongly recommend to refer set 1 as a prerequisite of this post.Treap (A Randomized Binary Search Tree)In this post, implementations of search, insert and delete are discussed.Search: Same as standard BST search. Priority is not considered for search. C/C++ Code // C function to search a given key in a given BST TreapNode* search(TreapNode* roo
15+ min read
Search, Insert, and Delete in an Sorted Array | Array Operations
How to Search in a Sorted Array? In a sorted array, the search operation can be performed by using binary search. Below is the implementation of the above approach: C/C++ Code // C++ program to implement binary search in sorted array #include &lt;bits/stdc++.h&gt; using namespace std; int binarySearch(int arr[], int low, int high, int key) { if (hi
15+ min read
Skip List - Efficient Search, Insert and Delete in Linked List
A skip list is a data structure that allows for efficient search, insertion and deletion of elements in a sorted list. It is a probabilistic data structure, meaning that its average time complexity is determined through a probabilistic analysis. In a skip list, elements are organized in layers, with each layer having a smaller number of elements th
6 min read
Search, Insert, and Delete in an Unsorted Array | Array Operations
In this post, a program to search, insert, and delete operations in an unsorted array is discussed. Search Operation:In an unsorted array, the search operation can be performed by linear traversal from the first element to the last element. Coding implementation of the search operation:[GFGTABS] C++ // C++ program to implement linear // search in u
15+ min read
Minimum time to write characters using insert, delete and copy operation
We need to write N same characters on a screen and each time we can insert a character, delete the last character and copy and paste all written characters i.e. after copy operation count of total written character will become twice. Now we are given time for insertion, deletion and copying. We need to output minimum time to write N characters on t
8 min read
Efficiently design Insert, Delete and Median queries on a set
Given an empty set initially and a number of queries on it, each possibly of the following types: Insert - Insert a new element ‘x’.Delete - Delete an existing element ‘x’.Median - Print the median element of the numbers currently in the set Example: Input : Insert 1 Insert 4 Insert 7 Median Output : The first three queries should insert 1, 4 and 7
15 min read
Add and Search Word - Data Structure Design
Design a data structure GFGDictionary that enables to support the feature of adding new words and searching for words. The GFGDictionary class should have the following implementation: GFGDictionary(): constructor to initialize the objectvoid insertWord(word): function to insert word into the data structure.bool findWord(word): function to return t
4 min read
Queries for rotation and Kth character of the given string in constant time
Given a string str, the task is to perform the following type of queries on the given string: (1, K): Left rotate the string by K characters.(2, K): Print the Kth character of the string. Examples: Input: str = "abcdefgh", q[][] = {{1, 2}, {2, 2}, {1, 4}, {2, 7}} Output: d e Query 1: str = "cdefghab" Query 2: 2nd character is d Query 3: str = "ghab
6 min read
Count Possible Decodings of a given Digit Sequence in O(N) time and Constant Auxiliary space
Given a digit sequence S, the task is to find the number of possible decodings of the given digit sequence where 1 represents 'A', 2 represents 'B' ... and so on up to 26, where 26 represents 'Z'. Examples: Input: S = "121" Output: 3 The possible decodings are "ABA", "AU", "LA" Input: S = "1234" Output: 3 The possible decodings are "ABCD", "LCD", "
6 min read
Javascript Program for Queries for rotation and Kth character of the given string in constant time
Given a string str, the task is to perform the following type of queries on the given string: (1, K): Left rotate the string by K characters.(2, K): Print the Kth character of the string. Examples: Input: str = "abcdefgh", q[][] = {{1, 2}, {2, 2}, {1, 4}, {2, 7}} Output: d e Query 1: str = "cdefghab" Query 2: 2nd character is d Query 3: str = "ghab
2 min read
Static Data Structure vs Dynamic Data Structure
Data structure is a way of storing and organizing data efficiently such that the required operations on them can be performed be efficient with respect to time as well as memory. Simply, Data Structure are used to reduce complexity (mostly the time complexity) of the code. Data structures can be two types : 1. Static Data Structure 2. Dynamic Data
4 min read
Sorted subsequence of size 3 in linear time using constant space
Given an array, the task is to find three elements of this array such that they are in sorted form i.e. for any three elements a[i], a[j] and a[k], they follow this relationship: a[i] &lt; a[j] &lt; a[k] where i &lt; j &lt; k. This problem must be solved using constant space or no extra space. This problem is already solved in linear time using lin
9 min read
Constant time range add operation on an array
Given an array of size N which is initialized with all zeros. We are given many ranges add queries, which should be applied to this array. We need to print the final updated array as our result. Examples: N = 6 Arr = [0, 0, 0, 0, 0, 0] rangeUpdate1 [0, 2], add 100 Arr = [100, 100, 100, 0, 0, 0] rangeUpdate1 [1, 5], add 100 Arr = [100, 200, 200, 100
7 min read
Find Index of given fibonacci number in constant time
We are given a Fibonacci number. The first few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ..... We have to find the index of the given Fibonacci number, i.e. like Fibonacci number 8 is at index 6. Examples : Input : 13Output : 7Input : 34Output : 9Method 1 (Simple) : A simple approach is to find Fibonacci numbers up to the
10 min read
What does Constant Time Complexity or Big O(1) mean?
Big O notation is a concept, in computer science and mathematics that allows us to analyse and describe the efficiency of algorithms for worst cases. It provides a way to measure how the runtime of an algorithm or function changes as the input size grows. In this article we'll explore the idea of O(1) complexity, what it signifies and provide examp
5 min read
Queries to insert, delete one occurrence of a number and print the least and most frequent element
Given Q queries of type 1, 2, 3 and 4 as described below. Type-1: Insert a number to the list.Type-2: Delete only one occurrence of a number if exists.Type-3: Print the least frequent element, if multiple elements exist then print the greatest among them.Type-4: Print the most frequent element, if multiple elements exist then print the smallest amo
14 min read
Insert and delete element from spirally sorted matrix
Given a Spirally Sorted 2D matrix mat[][] of size NXN, remove an element R and add another element A with only one traversal and without disrupting the sorted order. In other words, the matrix should remain in a spirally sorted form after both removal and addition operations. Note: R will always be present in the Matrix to remove Examples: Input: m
15+ min read
Implement the insert and delete functions on Priority queue without Array
A priority Queue is a type of queue in which every element is associated with a priority and is served according to its priority. We will use two popular data structures for implementing priority queues without arrays - Fibonacci HeapBinomial HeapFibonacci Heap:Fibonacci heap is a heap data structure that is composed of a collection of min-heap-ord
15+ min read
Search N elements in an unbalanced Binary Search Tree in O(N * logM) time
Given an Unbalanced binary search tree (BST) of M nodes. The task is to find the N elements in the Unbalanced Binary Search Tree in O(N*logM) time. Examples: Input: search[] = {6, 2, 7, 5, 4, 1, 3}. Consider the below tree Output:FoundNot FoundFoundFoundFoundFoundNot Found Naive Approach: For each element, we will try to search for that element in
8 min read
Minimum cost to convert one given string to another using swap, insert or delete operations
Given two strings A and B of length N and M respectively, the task is to find the minimum cost to convert string A to B using the following operations: A character of string A can be swapped from another character of the same string. Cost = 0.A character can be deleted from string B or can be inserted in the string A. Cost = 1. Examples: Input: A =
6 min read
Time difference between expected time and given time
Given the initial clock time h1:m1 and the present clock time h2:m2, denoting hour and minutes in 24-hours clock format. The present clock time h2:m2 may or may not be correct. Also given a variable K which denotes the number of hours passed. The task is to calculate the delay in seconds i.e. time difference between expected time and given time. Ex
5 min read
2-3 Trees | (Search, Insert and Deletion)
In binary search trees we have seen the average-case time for operations like search/insert/delete is O(log N) and the worst-case time is O(N) where N is the number of nodes in the tree. Like other Trees include AVL trees, Red Black Tree, B tree, 2-3 Tree is also a height balanced tree. The time complexity of search/insert/delete is O(log N) . A 2-
4 min read
Introduction to Finger search tree Data Structure
A finger search tree is a data structure that is designed to allow for efficient search and access of data in a set or a sequence. It is a type of binary search tree that uses a "finger" or a reference to a particular element in the tree to quickly find and retrieve other elements. In this article, we will explore the types, advantages, disadvantag
15+ min read
How to insert a node in Binary Search Tree using Iteration
You are given a binary search tree (BST) and a value to insert into the tree. Print inorder traversal of the BST after the insertion.Example: Input:To the given BST insert 40 Output: Explanation:The new node 40 is a leaf node. Start searching from the root till a leaf node is hit, i.e while searching if a new value is greater than current node move
10 min read
Search insert position of K in a sorted array
Given a sorted array arr[] consisting of N distinct integers and an integer K, the task is to find the index of K, if it's present in the array arr[]. Otherwise, find the index where K must be inserted to keep the array sorted. Examples: Input: arr[] = {1, 3, 5, 6}, K = 5Output: 2Explanation: Since 5 is found at index 2 as arr[2] = 5, the output is
9 min read