-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
2 changed files
with
57 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# 136. Single Number | ||
|
||
Problem Link: https://leetcode.com/problems/single-number/ | ||
|
||
Easy | ||
|
||
Given a non-empty array of integers nums, every element appears twice except for one. Find that single one. | ||
|
||
You must implement a solution with a linear runtime complexity and use only constant extra space. | ||
|
||
Example 1: | ||
|
||
``` | ||
Input: nums = [2,2,1] | ||
Output: 1 | ||
``` | ||
|
||
Example 2: | ||
|
||
``` | ||
Input: nums = [4,1,2,1,2] | ||
Output: 4 | ||
``` | ||
|
||
Example 3: | ||
|
||
``` | ||
Input: nums = [1] | ||
Output: 1 | ||
``` | ||
|
||
Constraints: | ||
|
||
1 <= nums.length <= 3 * 104 | ||
-3 * 104 <= nums[i] <= 3 * 104 | ||
Each element in the array appears twice except for one element which appears only once. |
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,21 @@ | ||
func singleNumber(nums []int) int { | ||
// create hashmap | ||
// if value in hashset then pop value | ||
// the value that stays in the hashset is the answer | ||
m := make(map[int]int) | ||
|
||
for i := 0; i < len(nums); i++ { | ||
value := nums[i] | ||
_, keyExists := m[value] | ||
if keyExists { | ||
delete(m, value) | ||
continue | ||
} | ||
m[value] = 1 | ||
} | ||
answer := 0 | ||
for key, _ := range m { | ||
answer = key | ||
} | ||
return answer | ||
} |