-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsingleNumber.js
More file actions
34 lines (25 loc) · 640 Bytes
/
singleNumber.js
File metadata and controls
34 lines (25 loc) · 640 Bytes
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
/*
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
*/
//Answer//
/**
* @param {number[]} nums
* @return {number}
*/
var singleNumber = function(nums) {
return nums.filter(x=>nums.indexOf(x)===nums.lastIndexOf(x))[0]
};
//Or//
var singleNumber = function(nums) {
for (let i = 0; i < nums.length ; i++) {
if(nums.indexOf(nums[i])===nums.lastIndexOf(nums[i])){return nums[i]}
}
};