-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind-the-unique-number.js
More file actions
40 lines (29 loc) · 935 Bytes
/
find-the-unique-number.js
File metadata and controls
40 lines (29 loc) · 935 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
35
36
37
38
39
40
/*
There is an array with some numbers. All numbers are equal except for one. Try to find it!
findUniq([ 1, 1, 1, 2, 1, 1 ]) === 2
findUniq([ 0, 0, 0.55, 0, 0 ]) === 0.55
It’s guaranteed that array contains at least 3 numbers.
The tests contain some very huge arrays, so think about performance.
This is the first kata in series:
Find the unique number (this kata)
Find the unique string
Find The Unique
*/
function findUniq(arr) {
console.log('----')
var filteredArray = arr.filter(function(item, pos){
return arr.indexOf(item)== pos
})
console.log(filteredArray)
for(let i = 0; i < filteredArray.length; i++){
let indices = []
let idx = arr.indexOf(filteredArray[i]);
console.log(idx)
while (idx !== -1) {
indices.push(idx);
idx = arr.indexOf(filteredArray[i], idx + 1);
}
console.log(indices)
if (indices.length == 1) return arr[indices[0]]
}
}