C# Program For Removing Duplicates From An Unsorted Linked List
Last Updated :
24 Mar, 2023
Write a removeDuplicates() function that takes a list and deletes any duplicate nodes from the list. The list is not sorted.
For example if the linked list is 12->11->12->21->41->43->21 then removeDuplicates() should convert the list to 12->11->21->41->43.
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 the inner loop compares the picked element with the rest of the elements.
Thanks to Gaurav Saxena for his help in writing this code.
C#
// C# program to remove duplicates from
// unsorted linked list
using System;
class List_
{
Node head;
class Node
{
public int data;
public Node next;
public Node(int d)
{
data = d;
next = null;
}
}
/* Function to remove duplicates from
an unsorted linked list */
void remove_duplicates()
{
Node ptr1 = null,
ptr2 = null, dup = null;
ptr1 = head;
// Pick elements one by one
while (ptr1 != null &&
ptr1.next != null)
{
ptr2 = ptr1;
/* Compare the picked element with
rest of the elements */
while (ptr2.next != null)
{
// If duplicate then delete it
if (ptr1.data == ptr2.next.data)
{
// sequence of steps is important here
dup = ptr2.next;
ptr2.next = ptr2.next.next;
}
else
{
ptr2 = ptr2.next;
}
}
ptr1 = ptr1.next;
}
}
void printList(Node node)
{
while (node != null)
{
Console.Write(node.data + " ");
node = node.next;
}
}
// Driver Code
public static void Main(String[] args)
{
List_ list = new List_();
list.head = new Node(10);
list.head.next = new Node(12);
list.head.next.next = new Node(11);
list.head.next.next.next = new Node(11);
list.head.next.next.next.next = new Node(12);
list.head.next.next.next.next.next = new Node(11);
list.head.next.next.next.next.next.next
= new Node(10);
Console.WriteLine(
"Linked List_ before removing duplicates : ");
list.printList(list.head);
list.remove_duplicates();
Console.WriteLine("");
Console.WriteLine(
"Linked List_ after removing duplicates : ");
list.printList(list.head);
}
}
// This code is contributed by gauravrajput1
Output:
Linked list before removing duplicates:
10 12 11 11 12 11 10
Linked list after removing duplicates:
10 12 11
Time Complexity: O(n^2)
Auxiliary Space: O(1)
METHOD 2 (Use Sorting):
In general, Merge Sort is the best-suited sorting algorithm for sorting linked lists efficiently.
1) Sort the elements using Merge Sort. We will soon be writing a post about sorting a linked list. O(nLogn)
2) Remove duplicates in linear time using the algorithm for removing duplicates in sorted Linked List. O(n)
Please note that this method doesn't preserve the original order of elements.
Time Complexity: O(nLogn)
METHOD 3 (Use Hashing):
We traverse the link list from head to end. For every newly encountered element, we check whether it is in the hash table: if yes, we remove it; otherwise we put it in the hash table.
C#
// C# program to remove duplicates
// from unsorted linkedlist
using System;
using System.Collections.Generic;
class removeDuplicates
{
class node
{
public int val;
public node next;
public node(int val)
{
this.val = val;
}
}
// Function to remove duplicates from a
// unsorted linked list
static void removeDuplicate(node head)
{
// Hash to store seen values
HashSet<int> hs = new HashSet<int>();
// Pick elements one by one
node current = head;
node prev = null;
while (current != null)
{
int curval = current.val;
// If current value is seen before
if (hs.Contains(curval))
{
prev.next = current.next;
}
else
{
hs.Add(curval);
prev = current;
}
current = current.next;
}
}
// Function to print nodes in a
// given linked list
static void printList(node head)
{
while (head != null)
{
Console.Write(head.val + " ");
head = head.next;
}
}
// Driver code
public static void Main(String[] args)
{
// The constructed linked list is:
// 10->12->11->11->12->11->10
node start = new node(10);
start.next = new node(12);
start.next.next = new node(11);
start.next.next.next = new node(11);
start.next.next.next.next = new node(12);
start.next.next.next.next.next = new node(11);
start.next.next.next.next.next.next = new node(10);
Console.WriteLine("Linked list before removing " +
"duplicates :");
printList(start);
removeDuplicate(start);
Console.WriteLine("Linked list after removing " +
"duplicates :");
printList(start);
}
}
// This code is contributed by amal kumar choubey
Output:
Linked list before removing duplicates:
10 12 11 11 12 11 10
Linked list after removing duplicates:
10 12 11
Thanks to bearwang for suggesting this method.
Time Complexity: O(n) on average (assuming that hash table access time is O(1) on average).
Please refer complete article on Remove duplicates from an unsorted linked list for more details!
Similar Reads
C Program For Removing Duplicates From A Sorted Linked List Write a function that takes a list sorted in non-decreasing order and deletes any duplicate nodes from the list. The list should only be traversed once. For example if the linked list is 11->11->11->21->43->43->60 then removeDuplicates() should convert the list to 11->21->43-
5 min read
C# Program For Removing Duplicates From A Sorted Linked List Write a function that takes a list sorted in non-decreasing order and deletes any duplicate nodes from the list. The list should only be traversed once. For example if the linked list is 11->11->11->21->43->43->60 then removeDuplicates() should convert the list to 11->21->43-
8 min read
C Program to Remove Duplicates from Sorted Array In this article, we will learn how to remove duplicates from a sorted array using the C program.The most straightforward method is to use the two-pointer approach which uses two pointers: one pointer to iterate over the array and other to track duplicate elements. In sorted arrays, duplicates are ad
4 min read
C Program For Writing A Function To Delete A Linked List Algorithm For C:Iterate through the linked list and delete all the nodes one by one. The main point here is not to access the next of the current pointer if the current pointer is deleted. Implementation: C // C program to delete a linked list #include<stdio.h> #include<stdlib.h> #includ
2 min read
C Program For Merge Sort For Doubly Linked List Given a doubly linked list, write a function to sort the doubly linked list in increasing order using merge sort.For example, the following doubly linked list should be changed to 24810 Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Merge sort for singly linked l
3 min read
C Program For Searching An Element In A Linked List Write a function that searches a given key 'x' in a given singly linked list. The function should return true if x is present in linked list and false otherwise. bool search(Node *head, int x) For example, if the key to be searched is 15 and linked list is 14->21->11->30->10, then functi
4 min read
C Program For Union And Intersection Of Two Linked Lists Write a C program for a given two Linked Lists, create union and intersection lists that contain union and intersection of the elements present in the given lists. The order of elements in output lists doesn't matter.Example: Input:List1: 10->15->4->20List2: 8->4->2->10Output:Inter
10 min read
C Program For Finding Intersection Of Two Sorted Linked Lists Given two lists sorted in increasing order, create and return a new list representing the intersection of the two lists. The new list should be made with its own memory â the original lists should not be changed. Example: Input: First linked list: 1->2->3->4->6 Second linked list be 2-
9 min read
C# Program For Finding Intersection Of Two Sorted Linked Lists Given two lists sorted in increasing order, create and return a new list representing the intersection of the two lists. The new list should be made with its own memory â the original lists should not be changed. Example: Input: First linked list: 1->2->3->4->6 Second linked list be 2->4->6->8, Ou
4 min read
C program to create copy of a singly Linked List using Recursion Given a pointer to the head node of a Linked List, the task is to create a copy of the linked list using recursion. Examples:: Input: Head of following linked list1->2->3->4->NULLOutput: Original list: 1 -> 2 -> 3 -> 4 -> NULLDuplicate list: 1 -> 2 -> 3 -> 4 -> NU
3 min read