โจfind couple
const {log} = console;
// if a+b == 9, define (a,b) a "couple"
Array.prototype.findCouple = function(){
let mate = {}; // hash table
for(const a of this){
if(mate[a] !== undefined) return [a, mate[a]];
mate[9-a] = a;
}
return null;
};
// ------------ log ------------
[
[1,2,3,4,5,6].findCouple(), // [ 5, 4 ]
[1,2,3,4,6].findCouple(), // [ 6, 3 ]
[1,2,5,6,10].findCouple(), // null
].forEach(x => log(x));
Last updated