Created
October 5, 2020 00:44
-
-
Save FSou1/5926b75b813deb87c894c23997ba51c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.codewars.com/kata/523f5d21c841566fde000009 | |
// Solution 1 | |
function arrayDiff(a, b) { | |
var result = []; | |
for(var n of a) { | |
if(!b.includes(n)) { | |
result.push(n); | |
} | |
} | |
return result; | |
} | |
// Solution 2 | |
function arrayDiff(a, b) { | |
var result = []; | |
var obj = getObject(b); | |
for(var n of a) { | |
if(!obj[n]) { | |
result.push(n); | |
} | |
} | |
return result; | |
} | |
function getObject(array) { | |
var obj = {}; | |
for(var n of array) { | |
obj[n] = obj[n] ? obj[n] + 1 : 1; | |
} | |
return obj; | |
} | |
// Solution 3 | |
function arrayDiff(a, b) { | |
var obj = getObject(b); | |
return a.filter(e => !obj[e]); | |
} | |
function getObject(array) { | |
var obj = {}; | |
for(var n of array) { | |
obj[n] = obj[n] ? obj[n] + 1 : 1; | |
} | |
return obj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment