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.
Consider the following custom Data structure:
Insert(1): [4, 1]
Insert(4): [4, 1, 4]
First_Non_Repeating Element: 1
- Doubly Linked-List: to keep track of the non-repeating elements in the array.
- Hash-map To keep track of the occurence of the elements and the address of non-repeating elements in the Doubly Linked-List
Below is the illustration of the operations:
- Insertion: Insert an element into the array and check the frequency of the element into the map. If it is previously occurred then remove the element from the Doubly Linked List with the help of the addresses stored in the hash-map. Finally, increase the occurrence of the element into the hash-map.
- First Non-repeating element: First Non-repeating element of the array will be the first element of the Doubly Linked List.
Advantages of this Data-structure:
- Insertion and First Non-repeating element in O(1) time.
Disadvantages of this Data-structure:
- Cannot Keep track of the order of the elements.
- Custom Data-structures will need custom Hasp-map to store the elements into the map.
- Memory Inefficient
Below is the implementation of the above approach:
- Design a data structure for LRU Cache
- Data Structure design to perform required operations
- Skip List | Set 2 (Insertion)
- Insertion in Unrolled Linked List
- Recursive insertion and traversal linked list
- Doubly Linked List | Set 1 (Introduction and Insertion)
- Insertion Sort for Singly Linked List
- Circular Singly Linked List | Insertion
- Find the time which is palindromic and comes after the given time
- Doubly Circular Linked List | Set 1 (Introduction and Insertion)
- Insertion in a sorted circular linked list when a random pointer is given
- Insertion at Specific Position in a Circular Doubly Linked List
- Element in a matrix starting from which anti-clockwise traversal ends at the last element
- Maximize the number of indices such that element is greater than element to its left
- Check if minimum element in array is less than or equals half of every other element
- Sum of (maximum element - minimum element) for all the subsets of an array.
- Merge Sort vs. Insertion Sort
- Applications of linked list data structure
- Create a Circular List Structure For Given Value K Using Recursion
- Stack Data Structure (Introduction and Program)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.