-
Notifications
You must be signed in to change notification settings - Fork 40
/
SingleNumber.js
45 lines (39 loc) · 1.1 KB
/
SingleNumber.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
// Source : https://oj.leetcode.com/problems/single-number/
// Author : Dean Shi
// Date : 2015-05-29
/**********************************************************************************
*
* Given an 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?
*
*
**********************************************************************************/
/**
* @param {number[]} nums
* @return {number}
*/
var singleNumber = function(nums) {
// Solution 1:
// Runtime: 164 ms
return nums.reduce(function(pre, cur) {
return pre ^ cur;
});
// Solution 2:
// Runtime: 140 ms
// var res;
// for (var i in nums) {
// res ^= nums[i];
// }
// return res;
// Solution 3:
// Runtime: 128 ms
// var res = nums[0];
// for (var i = 1, l = nums.length; i < l; ++i) {
// res ^= nums[i];
// }
// return res;
};
var testCase = [1, 1, 2, 2, 3];
console.log(singleNumber(testCase) === 3); // true