https://leetcode.com/problems/single-element-in-a-sorted-array/description/
int singleNonDuplicate(vector<int>& nums) {
for(int i=1;i<nums.size();i+=2) {
if(nums[i] != nums[i-1]) return nums[i-1];
}
return nums.back();
}
or use XOR
int singleNonDuplicate(vector<int>& nums) {
int res = 0;
for(int n : nums) {
res ^= n;
}
return res;
}
No comments:
Post a Comment