Skip to content

Commit

Permalink
Find single number in the array
Browse files Browse the repository at this point in the history
  • Loading branch information
nirmalnishant645 authored Jan 25, 2020
1 parent 8645566 commit 89bd73c
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions 0136-Single-Number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'''
Given a non-empty array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,1]
Output: 1
Example 2:
Input: [4,1,2,1,2]
Output: 4
'''
class Solution:
def singleNumber(self, nums: List[int]) -> int:
if len(nums) == 1:
return nums[0]
while True:
a = nums.pop(0)
if a in nums:
nums.append(a)
else:
return a

0 comments on commit 89bd73c

Please sign in to comment.