You are given an array of integers, nums , where each element occurs exactly three times, except one that occurs only once.Please find and return the element that appears only once.
Example 1:
Input: nums = [2,2,3,2]
Output: 3
Example 2:
Input: nums = [0,1,0,1,0,1,99]
Output: 99
We can count the number of occurrences of each element in the array using a hash map.For each key-value pair in a hash map, the key represents an element and the value represents the number of times it occurred.
After the statistics are complete, we traverse the hash map to find the elements that appear only once.
class Solution:def singleNumber(self, nums: List[int]) -> int:freq = collections.Counter(nums)for k,v in freq.items():span>if v==1:return k