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:
- Find the time which is palindromic and comes after the given time
- Data Structure design to perform required operations
- Design a data structure for LRU Cache
- Doubly Circular Linked List | Set 1 (Introduction and Insertion)
- Doubly Linked List | Set 1 (Introduction and Insertion)
- Recursive insertion and traversal linked list
- Element in a matrix starting from which anti-clockwise traversal ends at the last element
- Circular Singly Linked List | Insertion
- Insertion Sort for Singly Linked List
- Skip List | Set 2 (Insertion)
- Insertion in Unrolled Linked List
- Insertion at Specific Position in a Circular Doubly Linked List
- Insertion in a sorted circular linked list when a random pointer is given
- Merge Sort vs. Insertion Sort
- Find the minimum time after which one can exchange notes
- Sum of first N natural numbers which are not powers of K
- Minimum in an array which is first decreasing then increasing
- Maximum and Minimum element of a linked list which is divisible by a given number k
- Count of subarrays which start and end with the same element
- Rearrange a linked list in to alternate first and last element
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.