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.

**Initialization:**

We create an array of size n and initialize all elements as absent.

`void` `initialize(` `int` `n) ` `{ ` ` ` `bool` `A[n]; ` ` ` `for` `(` `int` `i = 0; i<n; i++) ` ` ` `A[i] = {0}; ` `// or A[n] = {false}; ` `} ` |

*chevron_right*

*filter_none*

**Insertion of an element:**

`void` `insert(unsigned i) ` `{ ` ` ` `/* set the value at index i to true */` ` ` `A[i] = 1; ` `// Or A[i] = true; ` `} ` |

*chevron_right*

*filter_none*

**Deletion of an element:**

`void` `delete` `(unsigned i) ` `{ ` ` ` `/* make the value at index i to 0 */` ` ` `A[i] = 0; ` `// Or A[i] = false; ` `} ` |

*chevron_right*

*filter_none*

** Finding an element:**

`// Returns true if 'i' is present, else false ` `bool` `find(unsigned i) ` `{ ` ` ` `return` `A[i]; ` `} ` |

*chevron_right*

*filter_none*

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.

## Recommended Posts:

- Static Data Structure vs Dynamic Data Structure
- Stack Data Structure (Introduction and Program)
- Sort a 2D vector diagonally using Map Data Structure
- Inversion Count using Policy Based Data Structure
- Remove duplicates from unsorted array using Map data structure
- Remove duplicates from unsorted array using Set data structure
- Make all the elements of array even with given operations
- Minimum gcd operations to make all array elements one
- Maximum sum of all elements of array after performing given operations
- Check if all array elements can be removed by the given operations
- Check if all array elements can be converted to K using given operations
- Minimum number of operations on an array to make all elements 0
- Make all the array elements odd with minimum operations of given type
- Minimum delete operations to make all elements of array same
- Minimum operations to make all elements equal using the second array
- Find if it is possible to make all elements of an array equal by the given operations
- Minimum length subarray containing all unique elements after Q operations
- Number of array elements derivable from D after performing certain operations
- Minimum increment by k operations to make all elements equal
- Minimum no. of operations required to make all Array Elements Zero