Find the first duplicate element in the linked list
Last Updated :
21 Jun, 2021
Given a linked list. Find the first element from the left which appears more than once. If all the elements are unique then print -1.
Examples:
Input : 1 2 3 4 3 2 1
Output : 1
In this linked list the element 1 occurs two times
and it is the first element to satisfy the condition.
Hence, the answer is 1.
Input : 1 2, 3, 4, 5
Output : -1
All the elements are unique. Hence, the answer is -1.
Approach:
- Count the frequency of all the elements of the linked list using a map.
- Now, traverse the linked list again to find the first element from the left whose frequency is greater than 1.
- If no such element exists then print -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* next;
};
void append(Node** head_ref, int new_data)
{
Node* new_node = new Node();
Node* last = *head_ref;
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL) {
*head_ref = new_node;
return ;
}
while (last->next != NULL)
last = last->next;
last->next = new_node;
return ;
}
int getFirstDuplicate(Node* node)
{
unordered_map< int , int > mp;
Node* head = node;
while (node != NULL) {
mp[node->data]++;
node = node->next;
}
node = head;
while (node != NULL) {
if (mp[node->data] > 1)
return node->data;
node = node->next;
}
return -1;
}
int main()
{
Node* head = NULL;
append(&head, 6);
append(&head, 2);
append(&head, 1);
append(&head, 6);
append(&head, 2);
append(&head, 1);
cout << getFirstDuplicate(head);
return 0;
}
|
Java
import java.util.*;
class GFG{
static class Node
{
int data;
Node next;
};
static Node head_ref;
static void append( int new_data)
{
Node new_node = new Node();
Node last = head_ref;
new_node.data = new_data;
new_node.next = null ;
if (head_ref == null )
{
head_ref = new_node;
return ;
}
while (last.next != null )
last = last.next;
last.next = new_node;
return ;
}
static int getFirstDuplicate(Node node)
{
HashMap<Integer,
Integer> mp = new HashMap<Integer,
Integer>();
Node head = node;
while (node != null )
{
if (mp.containsKey(node.data))
mp.put(node.data,
mp.get(node.data) + 1 );
else
mp.put(node.data, 1 );
node = node.next;
}
node = head;
while (node != null )
{
if (mp.get(node.data) > 1 )
return node.data;
node = node.next;
}
return - 1 ;
}
public static void main(String[] args)
{
head_ref = null ;
append( 6 );
append( 2 );
append( 1 );
append( 6 );
append( 2 );
append( 1 );
System.out.print(
getFirstDuplicate(head_ref));
}
}
|
Python3
class Node :
def __init__( self ):
self .data = 0
self . next = None
def append(head_ref, new_data):
new_node = Node()
last = head_ref
new_node.data = new_data
new_node. next = None
if (head_ref = = None ) :
head_ref = new_node
return head_ref
while (last. next ! = None ):
last = last. next
last. next = new_node
return head_ref
def getFirstDuplicate(node):
mp = dict ()
head = node
while (node ! = None ) :
mp[node.data] = mp.get(node.data, 0 ) + 1
node = node. next
node = head
while (node ! = None ) :
if (mp[node.data] > 1 ):
return node.data
node = node. next
return - 1
head = None
head = append(head, 6 )
head = append(head, 2 )
head = append(head, 1 )
head = append(head, 6 )
head = append(head, 2 )
head = append(head, 1 )
print (getFirstDuplicate(head))
|
C#
using System;
using System.Collections.Generic;
class GFG{
public class Node
{
public int data;
public Node next;
};
static Node head_ref;
static void append( int new_data)
{
Node new_node = new Node();
Node last = head_ref;
new_node.data = new_data;
new_node.next = null ;
if (head_ref == null )
{
head_ref = new_node;
return ;
}
while (last.next != null )
last = last.next;
last.next = new_node;
return ;
}
static int getFirstDuplicate(Node node)
{
Dictionary< int ,
int > mp =
new Dictionary< int ,
int >();
Node head = node;
while (node != null )
{
if (mp.ContainsKey(node.data))
mp[node.data]++;
else
mp.Add(node.data, 1);
node = node.next;
}
node = head;
while (node != null )
{
if (mp[node.data] > 1)
return node.data;
node = node.next;
}
return -1;
}
public static void Main(String[] args)
{
head_ref = null ;
append(6);
append(2);
append(1);
append(6);
append(2);
append(1);
Console.Write(
getFirstDuplicate(head_ref));
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .next = null ;
}
};
var head_ref;
function append(new_data)
{
var new_node = new Node();
var last = head_ref;
new_node.data = new_data;
new_node.next = null ;
if (head_ref == null )
{
head_ref = new_node;
return ;
}
while (last.next != null )
last = last.next;
last.next = new_node;
return ;
}
function getFirstDuplicate(node)
{
var mp = new Map();
var head = node;
while (node != null )
{
if (mp.has(node.data))
mp.set(node.data , mp.get(node.data)+1);
else
mp.set(node.data, 1);
node = node.next;
}
node = head;
while (node != null )
{
if (mp.get(node.data) > 1)
return node.data;
node = node.next;
}
return -1;
}
head_ref = null ;
append(6);
append(2);
append(1);
append(6);
append(2);
append(1);
document.write(getFirstDuplicate(head_ref));
</script>
|
Time Complexity: O(N)
Share your thoughts in the comments
Please Login to comment...