-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
find-the-parity-outlier.js
125 lines (111 loc) · 3.1 KB
/
find-the-parity-outlier.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
function findOutlier(integers){
// a place to keep track of the number of odd numbers
let odds = 0;
// a place to keep track of the number of even numbers
let evens = 0;
let lastFoundEven;
let lastFoundOdd;
// iterate over the array
for (let i = 0; i < integers.length; i++) {
const currentValue = integers[i];
// if the current value is even - increment evens
if (currentValue % 2 === 0) {
evens++;
lastFoundEven = currentValue;
} else {
// else - increment odds
odds++;
lastFoundOdd = currentValue;
}
// if number of evens is greater than 1 and number of odds is 1
if (evens > 1 && odds === 1) {
// we found it
return lastFoundOdd;
} else if (odds > 1 && evens === 1) {
// else if number of odds is greater than 1 and number of evens is 1
// we found it!
return lastFoundEven;
}
}
}
function findOutlier(integers) {
// have a place to store the odds
let odds = [];
// have a place to store the evens
let evens = [];
// iterate over the integers
for (let i = 0; i < integers.length; i++) {
const currentValue = integers[i];
// if even
if (currentValue % 2 === 0) {
// push into evens
evens.push(currentValue);
} else {
// if odd
// push into odds
odds.push(currentValue);
}
if (evens.length > 1 && odds.length === 1) {
// we found it
return odds[0];
} else if (odds.length > 1 && evens.length === 1) {
// else if number of odds is greater than 1 and number of evens is 1
// we found it!
return evens[0];
}
}
// whichever array is length 1, return its first value
}
function findOutlier(integers) {
// have a place to store the odds
let odds = [];
// have a place to store the evens
let evens = [];
// iterate over the integers
for (let i = 0; i < integers.length; i++) {
const currentValue = integers[i];
// if even
if (currentValue % 2 === 0) {
// push into evens
evens.push(currentValue);
} else {
// if odd
// push into odds
odds.push(currentValue);
}
}
// whichever array is length 1, return its first value
return odds.length === 1 ? odds[0] : evens[0];
}
function findOutlier(integers) {
const {odds, evens} = integers.reduce(({odds, evens}, currentValue) => {
if (currentValue % 2 === 0) {
evens.push(currentValue);
} else {
odds.push(currentValue);
}
return {
odds,
evens
};
}, {
odds: [],
evens: []
});
return odds.length === 1 ? odds[0] : evens[0];
}
function findOutlier(integers) {
const evens = integers.filter(i => i % 2 === 0);
const odds = integers.filter(i => i % 2 !== 0);
return odds.length === 1 ? odds[0] : evens[0];
}
// Thanks! meiamsome
function findOutlier(num){
const parity = num[2 * ((num[0] ^ num[1]) & 0x1)] & 0x1;
return num.find(i => (i ^ parity) & 0x1);
}
console.log(findOutlier([0, 1, 2]), 1);
console.log(findOutlier([1, 2, 3]), 2);
console.log(findOutlier([2,6,8,10,3]), 3);
console.log(findOutlier([0,0,3,0,0]), 3);
console.log(findOutlier([1,1,0,1,1]), 0);