Skip to content

Commit e76c115

Browse files
authored
Single number js (#225)
* Single Number JavaScript * single number JavaScript added tests
1 parent 87caedc commit e76c115

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

JavaScript/single-number.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Given a non-empty array of integers, every element appears twice except for
3+
* one. Find that single one.
4+
*
5+
* Note:
6+
*
7+
* Your algorithm should have a linear runtime complexity. Could you implement
8+
* it without using extra memory?
9+
*
10+
* Example 1:
11+
*
12+
* Input: [2,2,1] Output: 1
13+
*
14+
* Example 2:
15+
*
16+
* Input: [4,1,2,1,2] Output: 4
17+
*
18+
*/
19+
20+
/**
21+
* @param {number[]} nums
22+
* @return {number}
23+
*/
24+
var singleNumber = function(nums) {
25+
result= []
26+
nums.sort()
27+
28+
nums.forEach(element => {
29+
if (result.indexOf(element) == -1){
30+
result.push(element)
31+
}else{
32+
result.splice(result.indexOf(element),1)
33+
}
34+
});
35+
36+
return result[0]
37+
};
38+
39+
40+
41+
//------- Test cases -----------------
42+
// Input: nums = [2,2,1]
43+
// Output: 1
44+
console.log(`Example 01 = ${singleNumber([2,2,1])} expected Output 1.`)
45+
46+
// Input: nums = [4,1,2,1,2]
47+
// Output: 4
48+
console.log(`Example 02 = ${singleNumber([4,1,2,1,2])} expected Output 4.`)
49+
50+
// Input: nums = [1]
51+
// Output: 1
52+
console.log(`Example 03 = ${singleNumber([1])} expected Output 1.`)

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
8484

8585
| # | Title | Solution | Time | Space | Difficulty | Tag | Tutorial |
8686
| ---- | ------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- | ------ | ------ | ---------- | --- | ---------------------------------------- |
87-
| 136 | [Single Number](https://leetcode.com/problems/single-number/) | [Java](./Java/single-number.java) <br> [Python](./Python/single-number.py) <br> [C++](./C++/Single-Number.cpp) | _O(n)_ | _O(1)_ | Easy | | Using XOR |
87+
| 136 | [Single Number](https://leetcode.com/problems/single-number/) | [Java](./Java/single-number.java) <br> [Python](./Python/single-number.py) <br> [C++](./C++/Single-Number.cpp) <br> [JavaScript](./JavaScript/single-number.js) | _O(n)_ | _O(1)_ | Easy | | Using XOR |
8888
| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [Python](./Python/single-number-ii.py)<br/> [C++](./C++/Single-Number-II.cpp) | _O(n)_ | _O(1)_ | Medium | | |
8989
| 260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [Python](./Python/single-number-iii.py) <br/> [C++](./C++/Single-Number-III.cpp) | _O(n)_ | _O(1)_ | Medium | | |
9090
| 371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [Java](./Java/Sum_of_two_integers.java) | _O(1)_ | _O(1)_ | Medium |

0 commit comments

Comments
 (0)