Find unique elements in linked list
Last Updated :
19 Dec, 2022
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 then 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.
Below is the Implementation of this
C++
// 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;
}
Java
// Java Program to Find the Unique elements
// in linked lists
import java.util.*;
class GFG
{
/* Linked list node */
static class Node
{
int data;
Node next;
};
static Node head;
/* Function to insert a node at the
beginning of the linked list */
static void push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
head = head_ref;
}
// function to Find the unique elements
// in linked lists
static void uniqueElements(Node head)
{
// Initialize hash array that store the
// frequency of each element of list
HashMap<Integer,
Integer> hash = new HashMap<Integer,
Integer>();
for (Node temp = head;
temp != null; temp = temp.next)
{
if(hash.containsKey(temp.data))
{
hash.put(temp.data,
hash.get(temp.data) + 1);
}
else
{
hash.put(temp.data, 1);
}
}
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.get(temp.data) == 1)
{
System.out.print(temp.data + " ");
count++;
}
}
// If No unique element in list
if (count == 0)
System.out.print(" No Unique Elements ");
}
// Driver Code
public static void main(String[] args)
{
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);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 Program to Find the Unique elements in
# linked lists
import sys
import math
# Linked list node
class Node:
def __init__(self,data):
self.data = data
self.next = None
# Function to insert a node at the beginning of
# the linked list
def push(head,data):
if not head:
return Node(data)
temp = Node(data)
temp.next = head
head = temp
return head
# function to Find the unique elements in linked lists
def uniqueElements(head):
# Initialize hash array that store the
# frequency of each element of list
_map = {}
temp = head
while(temp):
d = temp.data
if d in _map:
_map[d]=_map.get(d)+1
else:
_map[d] = 1
temp = temp.next
count = 0
for i in _map:
# Check whether the frequency of current
# element is 1 or not
if _map.get(i) == 1:
count += 1
print("{} ".format(i),end="")
# If No unique element in list
if count == 0:
print("No Unique Elements")
# Driver program to test above
if __name__=='__main__':
# creating linked list
head = None
head = push(head,5)
head = push(head,4)
head = push(head,3)
head = push(head,5)
head = push(head,3)
head = push(head,2)
head = push(head,4)
head = push(head,4)
head = push(head,1)
uniqueElements(head)
# This code is Contributed by Vikash Kumar 37
C#
// C# Program to Find the Unique elements
// in linked lists
using System;
using System.Collections.Generic;
class GFG
{
/* Linked list node */
public class Node
{
public int data;
public Node next;
};
static Node head;
/* Function to insert a node at the
beginning of the linked list */
static void push(Node head_ref,
int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
head = head_ref;
}
// function to Find the unique elements
// in linked lists
static void uniqueElements(Node head)
{
// Initialize hash array that store the
// frequency of each element of list
Dictionary<int,
int> hash = new Dictionary<int,
int>();
for (Node temp = head;
temp != null; temp = temp.next)
{
if(hash.ContainsKey(temp.data))
{
hash[temp.data] = hash[temp.data] + 1;
}
else
{
hash.Add(temp.data, 1);
}
}
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)
{
Console.Write(temp.data + " ");
count++;
}
}
// If No unique element in list
if (count == 0)
Console.Write(" No Unique Elements ");
}
// Driver Code
public static void Main(String[] args)
{
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);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// JavaScript Program to Find the Unique elements
// in linked lists
/* Linked list node */
class Node {
constructor() {
this.data = 0;
this.next = null;
}
}
var head = null;
/* Function to insert a node at the
beginning of the linked list */
function push(head_ref, new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
head = head_ref;
}
// function to Find the unique elements
// in linked lists
function uniqueElements(head) {
// Initialize hash array that store the
// frequency of each element of list
var hash = {};
for (var temp = head; temp != null; temp = temp.next) {
if (hash.hasOwnProperty(temp.data)) {
hash[temp.data] = hash[temp.data] + 1;
} else {
hash[temp.data] = 1;
}
}
var count = 0;
for (var temp = head; temp != null; temp = temp.next) {
// Check whether the frequency of
// current element is 1 or not
if (hash[temp.data] == 1) {
document.write(temp.data + " ");
count++;
}
}
// If No unique element in list
if (count == 0) document.write(" No Unique Elements ");
}
// Driver Code
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);
// This code is contributed by rdtank.
</script>
Time Complexity : O(N)
Auxiliary Space : O(N)
Similar Reads
Find a peak element in Linked List
Given a linked list of integers, the task is to find a peak element in the given linked list. An element is a peak, if it is greater than or equals to its neighbors. For boundary elements, consider only one neighbor.Examples: Input : List = {1 -> 6 -> 8 -> 4 -> 12} Output : 8Explanation:
8 min read
Deletion in Linked List
Deleting a node in a Linked List is an important operation and can be done in three main ways: removing the first node, removing a node in the middle, or removing the last node.In this article, we will explore deletion operation on Linked List for all the above scenarios. Types of Deletion in Linked
3 min read
Find the Second Largest Element in a Linked List
Given a Linked list of integer data. The task is to write a program that efficiently finds the second largest element present in the Linked List. Examples: Input : List = 12 -> 35 -> 1 -> 10 -> 34 -> 1 Output : The second largest element is 34. Input : List = 10 -> 5 -> 10 Outpu
9 min read
Count minimum frequency elements in a linked list
Given a linked list containing duplicate elements. The task is to find the count of all minimum occurring elements in the given linked list. That is the count of all such elements whose frequency is minimum in the matrix. Examples: Input : 1-> 2-> 2-> 3 Output : 2 Explanation: 1 and 3 are e
8 min read
Majority element in a linked list
Given a linked list, find majority element. An element is called Majority element if it appears more than or equal to n/2 times where n is total number of nodes in the linked list. Examples: Input : 1->2->3->4->5->1->1->1->NULL Output : 1 Explanation 1 occurs 4 times Input :1
14 min read
Next greater element in the Linked List
Given a linked list L of integers, the task is to return a linked list of integers such that it contains next greater element for each element in the given linked list. If there doesn't any greater element for any element then insert 0 for it. Examples: Input: 2->1->3->0->5 Output: 3-
15+ min read
Find modular node in a linked list
Given a singly linked list and a number k, find the last node whose n%k == 0, where n is the number of nodes in the list. Examples: Input : list = 1->2->3->4->5->6->7 k = 3 Output : 6 Input : list = 3->7->1->9->8 k = 2 Output : 9Recommended PracticeModular NodeTry It!Ta
5 min read
Reverse first K elements of given linked list
Given a pointer to the head node of a linked list and a number K, the task is to reverse the first K nodes of the linked list. We need to reverse the list by changing links between nodes. check also Reversal of a linked list Examples: Input : 1->2->3->4->5->6->7->8->9->10-
9 min read
First common element in two linked lists
Given two Linked Lists, find the first common element between given linked list i.e., we need to find first node of the first list which is also present in the second list. Examples: Input : List1: 10->15->4->20 Lsit2: 8->4->2->10 Output : 10 Input : List1: 1->2->3->4 Lsit
6 min read
Find the balanced node in a Linked List
Given a linked list, the task is to find the balanced node in a linked list. A balanced node is a node where the sum of all the nodes on its left is equal to the sum of all the node on its right, if no such node is found then print -1. Examples: Input: 1 -> 2 -> 7 -> 10 -> 1 -> 6 -
8 min read