Practice › Linked List
Remove Duplicates from an Unsorted List
Easy · Linked List · Asked at Amazon, Microsoft, TCS, Infosys, Wipro
Remove every node whose value has already appeared earlier in the list, keeping the first occurrence of each value. Print the resulting list.
Input
First line: n. Second line: the n node values from head to tail.
Output
The list after removing duplicates, space-separated.
Constraints
- 1 ≤ n ≤ 10^5
- −10^9 ≤ values ≤ 10^9
Walk the list with a hash set of values already seen. When the next node’s value is in the set, unlink it: prev.next = cur.next.
Example 1
Input
5 5 2 2 4 5
Output
5 2 4
Example 2
Input
3 1 1 1
Output
1
Hints
- The list is not sorted, so duplicates need not be next to each other.
- Remember every value seen so far in a hash set.
- If the next node’s value is already in the set, unlink that node.
Approach
Walk the list keeping a pointer to the last kept node. For each following node: if its value is in the set, skip it by setting prev.next = cur.next; otherwise add the value to the set and move prev forward. The first occurrence of every value is kept, in its original place.
Complexity: O(n) expected time · O(n) space for the set
Solution
C++ solution
#include <bits/stdc++.h>
using namespace std;
struct Node {
long long val;
Node *next = nullptr;
Node(long long v) : val(v) {}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
Node dummy(0), *t = &dummy;
for (int i = 0; i < n; i++) {
long long x;
cin >> x;
t = t->next = new Node(x);
}
unordered_set<long long> seen;
Node *prev = dummy.next;
seen.insert(prev->val);
while (prev->next) {
if (seen.count(prev->next->val)) prev->next = prev->next->next;
else {
seen.insert(prev->next->val);
prev = prev->next;
}
}
for (Node *p = dummy.next; p; p = p->next) cout << p->val << (p->next ? " " : "\n");
return 0;
}Python solution
import sys
class Node:
def __init__(self, val):
self.val, self.next = val, None
data = list(map(int, sys.stdin.read().split()))
dummy = tail = Node(0)
for v in data[1 : 1 + data[0]]:
tail.next = Node(v)
tail = tail.next
prev = dummy.next
seen = {prev.val}
while prev.next:
if prev.next.val in seen:
prev.next = prev.next.next
else:
seen.add(prev.next.val)
prev = prev.next
out, p = [], dummy.next
while p:
out.append(p.val)
p = p.next
print(*out)Java solution
import java.io.*;
import java.util.*;
public class Main {
static class Node {
long val;
Node next;
Node(long v) { val = v; }
}
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
in.nextToken();
int n = (int) in.nval;
Node dummy = new Node(0), t = dummy;
for (int i = 0; i < n; i++) {
in.nextToken();
t = t.next = new Node((long) in.nval);
}
HashSet<Long> seen = new HashSet<>();
Node prev = dummy.next;
seen.add(prev.val);
while (prev.next != null) {
if (seen.contains(prev.next.val)) prev.next = prev.next.next;
else {
seen.add(prev.next.val);
prev = prev.next;
}
}
StringBuilder sb = new StringBuilder();
for (Node p = dummy.next; p != null; p = p.next) sb.append(p.val).append(p.next != null ? " " : "\n");
System.out.print(sb);
}
}Solve Remove Duplicates from an Unsorted List online · All practice problems