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 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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
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;
}
void uniqueElements( struct Node* head)
{
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) {
if (hash[temp->data] == 1) {
cout << temp->data << " " ;
count++;
}
}
if (count == 0)
cout << " No Unique Elements " ;
}
int main()
{
struct Node* head = NULL;
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
import java.util.*;
class GFG
{
static class Node
{
int data;
Node next;
};
static Node head;
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;
}
static void uniqueElements(Node head)
{
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)
{
if (hash.get(temp.data) == 1 )
{
System.out.print(temp.data + " " );
count++;
}
}
if (count == 0 )
System.out.print( " No Unique Elements " );
}
public static void main(String[] args)
{
head = null ;
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);
}
}
|
Python3
import sys
import math
class Node:
def __init__( self ,data):
self .data = data
self . next = None
def push(head,data):
if not head:
return Node(data)
temp = Node(data)
temp. next = head
head = temp
return head
def uniqueElements(head):
_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:
if _map.get(i) = = 1 :
count + = 1
print ( "{} " . format (i),end = "")
if count = = 0 :
print ( "No Unique Elements" )
if __name__ = = '__main__' :
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)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node head;
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;
}
static void uniqueElements(Node head)
{
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)
{
if (hash[temp.data] == 1)
{
Console.Write(temp.data + " " );
count++;
}
}
if (count == 0)
Console.Write( " No Unique Elements " );
}
public static void Main(String[] args)
{
head = null ;
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);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
var head = null ;
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 uniqueElements(head) {
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) {
if (hash[temp.data] == 1) {
document.write(temp.data + " " );
count++;
}
}
if (count == 0) document.write( " No Unique Elements " );
}
head = null ;
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);
</script>
|
Time Complexity : O(N)
Auxiliary Space : O(N)
Last Updated :
19 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...