-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path136.single-number.js
55 lines (54 loc) · 1000 Bytes
/
136.single-number.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* @lc app=leetcode id=136 lang=javascript
*
* [136] Single Number
*
* https://leetcode.com/problems/single-number/description/
*
* algorithms
* Easy (64.92%)
* Likes: 4384
* Dislikes: 158
* Total Accepted: 868.1K
* Total Submissions: 1.3M
* Testcase Example: '[2,2,1]'
*
* 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
*
*
*/
// @lc code=start
/**
* @param {number[]} nums
* @return {number}
*/
var singleNumber = function (nums) {
// 0101^0001 = 0100 = 4, same 1 cancel, different 1 remain
let res = 0;
//2,2 same cancel other
for (let i = 0; i < nums.length; i++) {
res = res ^ nums[i];
console.log({ res });
}
return res;
};
// @lc code=end