forked from cdeweyx/DS-Career-Resources
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
conordewey3
committed
Oct 20, 2018
1 parent
451c6e0
commit 0345af2
Showing
2 changed files
with
36 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
''' | ||
136. Single Number | ||
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 | ||
''' | ||
|
||
# Dictionary approach - O(n) time, O(n) space | ||
def singleNumber(self, nums): | ||
d = {} | ||
for item in nums: | ||
if item in d: | ||
d[item] += 1 | ||
else: | ||
d[item] = 1 | ||
|
||
for key, value in d.items(): | ||
if value == 1: | ||
return key |