136. Single Number

Given a non-empty array of integers num, each element appears twice except one. Find the one and only. You need to implement a solution with linear complexity at runtime and use only constant extra space. Example 1: Input: nums = [2,2,1] Output: 1 Example 2: Input: nums = [4,1,2,1,2] Output: 4 Example 3: Input: nums = [1] Output: 1 Limitations: 1 <= count.length <= 3 * 104 -3 * 104 <= nums[i] <= 3 * 104 Each element in the array appears twice except for one element which appears only once. Solution class Solution { public: int singleNumber(vector& nums) { unordered_map a; for(auto x: nums) a[x]++; for(auto z:a) if(z.second==1) return z.first; return -1; } };

Comments

Popular posts from this blog

1431. Kids With the Greatest Number of Candies

125. Valid Palindrome

771. Jewels and Stones