Question:
Given an array of integers, every element appears three times except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Solution:
1 class Solution { 2 public: 3 int singleNumber(vector<int>& nums) { 4 sort(nums.begin(),nums.end()); 5 for(int i=0;i<nums.size();i+=3) 6 { 7 if(nums[i]!=nums[i+1]) 8 { 9 return nums[i]; 10 } 11 } 12 } 13 };