# Find unique elements in linked list

Given a linked list. We need to find unique elements in the linked list i.e, those elements which are not repeated in the linked list or those elements whose frequency is 1. If No such elements are present in list so Print ” No Unique Elements”.

Examples:

Input : 1 -> 4 -> 4 -> 2 -> 3 -> 5 -> 3 -> 4 -> 5 Output :1 2 Input :4 -> 5 -> 2 -> 5 -> 1 -> 4 -> 1 -> 2 Output :No Unique Elements

** Method 1 (Using Two Loops) ** This is the simple way where two loops are used. Outer loop is used to pick the elements one by one and inner loop compares the picked element with rest of the elements. If Element is not equal to other elements than Print that Element. Time Complexity : O(N * n)

** Method 2 (Sorting) : ** Sort the elements using Merge Sort. O(n Log n). Now Traverse List in linear time and check if current element is not equal to previous element then Print O(N)

Please note that this method doesn’t preserve the original order of elements.

Time Complexity: O(NLogN)

**Method 3 (Hashing)**

We use the concept of Hash table Here, We traverse the link list from head to end. For every newly encountered element, we put it in the hash table after that we again traverse list and Print those elements whose frequency is 1.Time Complexity : O(N)

Below is the Implementation of this

`// C++ Program to Find the Unique elements in ` `// linked lists ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `/* Linked list node */` `struct` `Node { ` ` ` `int` `data; ` ` ` `struct` `Node* next; ` `}; ` ` ` `/* Function to insert a node at the beginning of ` ` ` `the linked list */` `void` `push(` `struct` `Node** head_ref, ` `int` `new_data) ` `{ ` ` ` `struct` `Node* new_node = ` `new` `Node; ` ` ` `new_node->data = new_data; ` ` ` `new_node->next = *head_ref; ` ` ` `*head_ref = new_node; ` `} ` ` ` `// function to Find the unique elements in linked lists ` `void` `uniqueElements(` `struct` `Node* head) ` `{ ` ` ` `// Initialize hash array that store the ` ` ` `// frequency of each element of list ` ` ` `unordered_map<` `int` `, ` `int` `> hash; ` ` ` ` ` `for` `(Node *temp=head; temp!=NULL; temp=temp->next) ` ` ` `hash[temp->data]++; ` ` ` ` ` `int` `count = 0; ` ` ` `for` `(Node *temp=head; temp!=NULL; temp=temp->next) { ` ` ` ` ` `// Check whether the frequency of current ` ` ` `// element is 1 or not ` ` ` `if` `(hash[temp->data] == 1) { ` ` ` `cout << temp->data << ` `" "` `; ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If No unique element in list ` ` ` `if` `(count == 0) ` ` ` `cout << ` `" No Unique Elements "` `; ` `} ` ` ` `// Driver program to test above ` `int` `main() ` `{ ` ` ` `struct` `Node* head = NULL; ` ` ` ` ` `// creating linked list ` ` ` `push(&head, 5); ` ` ` `push(&head, 4); ` ` ` `push(&head, 3); ` ` ` `push(&head, 5); ` ` ` `push(&head, 3); ` ` ` `push(&head, 2); ` ` ` `push(&head, 4); ` ` ` `push(&head, 4); ` ` ` `push(&head, 1); ` ` ` `uniqueElements(head); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

1 2

Time Complexity : O(N)

Auxiliary Space : O(N)

## Recommended Posts:

- Find smallest and largest elements in singly linked list
- Find minimum and maximum elements in singly Circular Linked List
- Partitioning a linked list around a given value and If we don't care about making the elements of the list "stable"
- Reverse first K elements of given linked list
- Pairwise swap elements of a given linked list
- Double elements and append zeros in linked list
- Count minimum frequency elements in a linked list
- Check if elements of Linked List are present in pair
- Replace even nodes of a doubly linked list with the elements of array
- Pairwise swap elements of a given linked list by changing links
- Sort the linked list in the order of elements appearing in the array
- Find the sum of last n nodes of the given Linked List
- Find a peak element in Linked List
- Find the product of last N nodes of the given Linked List
- Find the fractional (or n/k - th) node in linked list

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.