-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-1.js
35 lines (28 loc) · 1.04 KB
/
day-1.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
// In the toy factory of the North Pole, each toy has a unique identification number.
// However, due to an error in the toy machine, some numbers have been
// assigned to more than one toy.
// Find the first identification number that has been repeated, where
// the second occurrence has the smallest index!
// In other words, if there is more than one repeated number,
// you must return the number whose second occurrence appears first in the list.
// If there are no repeated numbers, return -1.
function findFirstRepeated(gifts) {
let test = [],
lowest = undefined;
for (let i = 0; i < gifts.length; i++) {
if (test.indexOf(gifts[i]) > -1) {
lowest = lowest || i;
console.log("Esto es i=",i +"Esto es Lowest=", lowest)
if (i < lowest) {
lowest = i;
console.log("Esto es i=",i +"Esto es Lowest=", lowest)
}
} else {
test.push(gifts[i]);
console.log("Esto es i=",i)
}
}
return lowest ? gifts[lowest] : -1;
}
gifts = [2, 1, 3, 5, 3, 2];
console.log(findFirstRepeated(gifts));