Problem
Consider:
list1: 1->2->3->4->4->5->6
list2: 1->3->6->4->2->8
Result:
list3: 1->2->3->4->6
- Create a hash table or set
- Go through
list1
, mark entries as you visit them.O(N)
- Go through
list2
, mark entries (with different flag) as you visit them.O(M)
- Traverse hash table and find all the entries with both flags. Create
list3
as you find entries.O(H)
Total Complexity: O(N)+ O(M) + O(Max(N,H,M)) => O(N)
Complexity Analysis