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++

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// A linked list node ` `class` `Node { ` `public` `: ` ` ` `int` `data; ` ` ` `Node* next; ` `}; ` ` ` `// Given a reference (pointer to pointer) ` `// to the head of a list and an int, ` `// appends a new node at the end ` `void` `append(Node** head_ref, ` `int` `new_data) ` `{ ` ` ` `// allocate node ` ` ` `Node* new_node = ` `new` `Node(); ` ` ` ` ` `Node* last = *head_ref; ` ` ` ` ` `// put in the data ` ` ` `new_node->data = new_data; ` ` ` ` ` `// This new node is going to be ` ` ` `// the last node, so make next of ` ` ` `// it as NULL ` ` ` `new_node->next = NULL; ` ` ` ` ` `// If the Linked List is empty, ` ` ` `// then make the new node as head ` ` ` `if` `(*head_ref == NULL) { ` ` ` `*head_ref = new_node; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Else traverse till the last node ` ` ` `while` `(last->next != NULL) ` ` ` `last = last->next; ` ` ` ` ` `// Change the next of last node ` ` ` `last->next = new_node; ` ` ` `return` `; ` `} ` ` ` `int` `getFirstDuplicate(Node* node) ` `{ ` ` ` ` ` `// Unordered map to store the ` ` ` `// frequency of elements ` ` ` `unordered_map<` `int` `, ` `int` `> mp; ` ` ` `Node* head = node; ` ` ` ` ` `// update frequency of all the elements ` ` ` `while` `(node != NULL) { ` ` ` `mp[node->data]++; ` ` ` `node = node->next; ` ` ` `} ` ` ` ` ` `node = head; ` ` ` ` ` `// the first node from the left which ` ` ` `// appears more than once is the answer ` ` ` ` ` `while` `(node != NULL) { ` ` ` `if` `(mp[node->data] > 1) ` ` ` `return` `node->data; ` ` ` `node = node->next; ` ` ` `} ` ` ` ` ` `// all the nodes are unique ` ` ` `return` `-1; ` `} ` ` ` `// driver code ` `int` `main() ` `{ ` ` ` `// Start with the empty list ` ` ` `Node* head = NULL; ` ` ` ` ` `// Insert element ` ` ` `append(&head, 6); ` ` ` `append(&head, 2); ` ` ` `append(&head, 1); ` ` ` `append(&head, 6); ` ` ` `append(&head, 2); ` ` ` `append(&head, 1); ` ` ` ` ` `cout << getFirstDuplicate(head); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of above approach ` ` ` `# Link list node ` `class` `Node : ` ` ` `def` `__init__(` `self` `): ` ` ` `self` `.data ` `=` `0` ` ` `self` `.` `next` `=` `None` ` ` `# Given a reference (pointer to pointer) ` `# to the head of a list and an int, ` `# appends a node at the end ` `def` `append(head_ref, new_data): ` ` ` ` ` `# allocate node ` ` ` `new_node ` `=` `Node() ` ` ` ` ` `last ` `=` `head_ref ` ` ` ` ` `# put in the data ` ` ` `new_node.data ` `=` `new_data ` ` ` ` ` `# This node is going to be ` ` ` `# the last node, so make next of ` ` ` `# it as None ` ` ` `new_node.` `next` `=` `None` ` ` ` ` `# If the Linked List is empty, ` ` ` `# then make the node as head ` ` ` `if` `(head_ref ` `=` `=` `None` `) : ` ` ` `head_ref ` `=` `new_node ` ` ` `return` `head_ref ` ` ` ` ` `# Else traverse till the last node ` ` ` `while` `(last.` `next` `!` `=` `None` `): ` ` ` `last ` `=` `last.` `next` ` ` ` ` `# Change the next of last node ` ` ` `last.` `next` `=` `new_node ` ` ` `return` `head_ref ` ` ` `def` `getFirstDuplicate(node): ` ` ` ` ` `# Unordered map to store the ` ` ` `# frequency of elements ` ` ` `mp ` `=` `dict` `() ` ` ` `head ` `=` `node ` ` ` ` ` `# update frequency of all the elements ` ` ` `while` `(node !` `=` `None` `) : ` ` ` `mp[node.data] ` `=` `mp.get(node.data, ` `0` `) ` `+` `1` ` ` `node ` `=` `node.` `next` ` ` ` ` `node ` `=` `head ` ` ` ` ` `# the first node from the left which ` ` ` `# appears more than once is the answer ` ` ` `while` `(node !` `=` `None` `) : ` ` ` `if` `(mp[node.data] > ` `1` `): ` ` ` `return` `node.data ` ` ` `node ` `=` `node.` `next` ` ` ` ` `# all the nodes are unique ` ` ` `return` `-` `1` ` ` `# Driver code ` ` ` `# Start with the empty list ` `head ` `=` `None` ` ` `# Insert element ` `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)) ` ` ` `# This code is contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

**Output:**

6

** Time Complexity : ** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Create new linked list from two given linked list with greater element at each node
- Rearrange a linked list in to alternate first and last element
- Move first element to end of a given Linked List
- Create a linked list from two linked lists by choosing max element at each position
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Merge a linked list into another linked list at alternate positions
- Check if a linked list is Circular Linked List
- Convert singly linked list into circular linked list
- Difference between Singly linked list and Doubly linked list
- Convert Singly Linked List to XOR Linked List
- Find first node of loop in a linked list
- Find the product of first k nodes of the given Linked List
- Find first non-repeating character in a given string using Linked List
- Find just strictly greater element from first array for each element in second array
- Find the Second Largest Element in a Linked List
- Find a peak element in Linked List
- Find the element in a linked list with frequency at least N/3
- First common element in two linked lists
- In-place Merge two linked lists without changing links of first 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.