Skip to content

Commit d66f0be

Browse files
committed
Merge branch 'release/1.1.0'
2 parents f1d7dac + 46047fc commit d66f0be

File tree

5 files changed

+57
-3
lines changed

5 files changed

+57
-3
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ npm run test
1717

1818
## Solutions
1919

20-
| # | Title | Difficulty | Topics |
21-
|--:| ------|------------|--------|
20+
| LeetCode # | Title | Difficulty | Topics |
21+
|-----------:|:------|:-----------|:-------|
2222
| 1 | [Two Sum](/problems/two-sum) | Easy | Array, hash table |
23+
| 136 | [Single Number](/problems/single-number) | Easy | Hash table, bit manipulation |

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "leetcode",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "My solutions for LeetCode problems.",
55
"main": "index.js",
66
"scripts": {

problems/single-number/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Two Sum
2+
3+
[Link to LeetCode page](https://leetcode.com/problems/two-sum/)
4+
5+
Difficulty: Easy
6+
7+
Topics: Hash table, bit manipulation.
8+
9+
## Solution Explanation
10+
11+
This solution uses the [bitwise XOR](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#(Bitwise_XOR)) operator to return the unique value in the array.
12+
13+
To illustrate the bitwise XOR operation using the given Example 2 from LeetCode page:
14+
15+
```text
16+
Input: [4,1,2,1,2]
17+
18+
Bitwise XOR operation with 0 on the elements:
19+
0 ^ 4 ^ 1 ^ 2 ^ 1 ^ 2
20+
> 4
21+
```
22+
23+
We want to go through every element in the array, perform the bitwise XOR operation, and return a single value output. Instead of the conventional `for` loop, we use array's [`reduce()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce) function to achieve this, which is more succinct.
24+
25+
## Complexity Analysis
26+
27+
Time complexity: O(n) because we go through every elements in the array once.
28+
29+
Space complexity: O(1) because the memory used is always the same regardless of the number of elements.

problems/single-number/solution.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const singleNumber = (nums) => {
2+
return nums.reduce((acc, cur) => {
3+
return acc ^ cur
4+
}, 0)
5+
}
6+
7+
module.exports = singleNumber
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const singleNumber = require('./solution')
2+
3+
test('Sample 1', () => {
4+
const input = [2, 2, 1]
5+
6+
const result = singleNumber(input)
7+
8+
expect(result).toBe(1)
9+
})
10+
11+
test('Sample 2', () => {
12+
const input = [4, 1, 2, 1, 2]
13+
14+
const result = singleNumber(input)
15+
16+
expect(result).toBe(4)
17+
})

0 commit comments

Comments
 (0)