Single Number
Input: nums = [2,2,1]
Output: 1Input: nums = [4,1,2,1,2]
Output: 4Input: nums = [1]
Output: 1Solutions
π§ Cpp
Last updated
Input: nums = [2,2,1]
Output: 1Input: nums = [4,1,2,1,2]
Output: 4Input: nums = [1]
Output: 1Last updated
class Solution {
public:
int singleNumber(vector<int>& nums)
{
for(int i=0; i < nums.size(); ++i)
{
for(int j=0; ; ++j)
if(nums[i] == nums[j] && i != j)
break;
else if (j == nums.size()-1)
return nums[i];
}
return 0;
}
};