Skip to content

Commit

Permalink
daily commit 2024-04-24
Browse files Browse the repository at this point in the history
  • Loading branch information
chirbard committed Apr 24, 2024
1 parent 3a4b287 commit beb6a8a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
36 changes: 36 additions & 0 deletions 2024-04-24/README.md
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.
21 changes: 21 additions & 0 deletions 2024-04-24/main.go
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
}

0 comments on commit beb6a8a

Please sign in to comment.