The first thing we have to do is to sort the given list nums. This is achieved using this:
After that we have to check every alternate number is equal to its right neighbor or not by incrementing loop variable i by 2.
If we don't find the two consecutive number same then we can break our loop and print the number which is nums[i].
If we have not encountered any number yet then our last number must be the single number and we can print that using nums[-1].
class Solution(object): def singleNumber(self, nums): """ :type nums: List[int] :rtype: int """ nums.sort() for i in range(0,len(nums)-1,2): if nums[i]!=nums[i+1]: return nums[i] break return nums[-1]