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
- Static Data Structure vs Dynamic Data Structure
- Stack Data Structure (Introduction and Program)
- Remove duplicates from unsorted array using Map data structure
- Remove duplicates from unsorted array using Set data structure
- Minimum gcd operations to make all array elements one
- Maximum sum of all elements of array after performing given operations
- Minimum delete operations to make all elements of array same
- Number of elements from the array which are reachable after performing given operations on D
- Minimum increment by k operations to make all elements equal
- Minimum number of operations on an array to make all elements 0
- Make all the array elements odd with minimum operations of given type
- Number of array elements derivable from D after performing certain operations
- Minimum no. of operations required to make all Array Elements Zero
- Minimum Bitwise XOR operations to make any two array elements equal
- Minimum Bitwise AND operations to make any two array elements equal