Suppose we have a list of numbers called nums, we have to remove numbers that appear multiple times in the list, we also have to maintain the order of the appearance in the original list.
So, if the input is like nums = [2, 4, 6, 1, 4, 6, 9], then the output will be [2, 1, 9], as these elements have appeared only once.
To solve this, we will follow these steps −
- dict := a new map
- for each i in nums, do
- if i is not in dict, then
- dict[i] := 0
- dict[i] := dict[i] + 1
- if i is not in dict, then
- return a list with all elements e in nums where dict[e] is 1
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, nums): dict = {} for i in nums: if i not in dict: dict[i] = 0 dict[i] = dict[i] + 1 return [k for k, v in dict.items() if v == 1] ob = Solution() nums = [2, 4, 6, 1, 4, 6, 9] print(ob.solve(nums))
Input
[2, 4, 6, 1, 4, 6, 9]
Output
[2, 1, 9]