Suppose we have a list of numbers called nums where each value occurs exactly three times except one value that occurs once. We have to find the unique value. We have to solve it in constant space.
So, if the input is like nums = [3, 3, 3, 8, 4, 4, 4], then the output will be 8
To solve this, we will follow these steps −
m := a map with different values and their frequencies
return the value with minimum frequency
Let us see the following implementation to get better understanding −
Example
from collections import Counter class Solution: def solve(self, nums): nums = Counter(nums) return min(nums, key=nums.get) ob = Solution() nums = [3, 3, 3, 8, 4, 4, 4] print(ob.solve(nums))
Input
[3, 3, 3, 8, 4, 4, 4]
Output
8