Propose a data structure for the following:
The data structure would hold elements from 0 to n-1. There is no order on the elements (no ascending/descending order requirement)
The complexity of the operations should be as follows:
* Insertion of an element – O(1)
* Deletion of an element – O(1)
* Finding an element – O(1)
We strongly recommend to minimize the browser and try this yourself first.
A boolean array works here. Array will have value ‘true’ at ith index if i is present, and ‘false’ if absent.
We create an array of size n and initialize all elements as absent.
Insertion of an element:
Deletion of an element:
Finding an element:
As an exercise, change the data structure so that it holds values from 1 to n instead of 0 to n-1.
This article is contributed by Sachin. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Static Data Structure vs Dynamic Data Structure
- Stack Data Structure (Introduction and Program)
- Remove duplicates from unsorted array using Map data structure
- Sort a 2D vector diagonally using Map Data Structure
- Inversion Count using Policy Based Data Structure
- Remove duplicates from unsorted array using Set data structure
- Common operations on various Data Structures
- Minimum Bitwise AND operations to make any two array elements equal
- Maximize difference between maximum and minimum array elements after K operations
- Minimum elements to change so that for an index i all elements on the left are -ve and all elements on the right are +ve
- Count of Array elements greater than all elements on its left and next K elements on its right
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Minimum number of increment/decrement operations such that array contains all elements from 1 to N
- Minimum delete operations to make all elements of array same
- Minimum number of increment-other operations to make all array elements equal.
- Minimum gcd operations to make all array elements one
- Minimum increment by k operations to make all elements equal
- Find the number of operations required to make all array elements Equal
- Minimum operations required to make all Array elements divisible by K
- Minimum no. of operations required to make all Array Elements Zero