The Only Nonrepeating Array Element
Problem statement
Given a nonempty integer array nums, return its only value that occurs exactly once.
The input contains exactly one such value. Every other distinct value occurs at least twice, and those repeated values may have different frequencies. The array is not necessarily sorted.
Function
onlyNonrepeatingElement(nums: int[]) → intExamples
Example 1
nums = [4,1,4,2,1]return = 2Only 2 occurs once.
Example 2
nums = [-1,8,8,8,-1,-1,5]return = 5The repeated values each occur three times; only 5 occurs once.
Example 3
nums = [9]return = 9A singleton has exactly one value occurring once.
Constraints
1 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9- Exactly one value has frequency 1; every other distinct value has frequency at least 2.