Created
October 1, 2015 00:15
-
-
Save timetocode/d7ef265234f622a9a0fb 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
function compareNumbers(a, b) { | |
return a - b | |
} | |
function arrayDiffs(a, b) { | |
a.sort(compareNumbers) | |
b.sort(compareNumbers) | |
//console.log('A', a) | |
//console.log('B', b) | |
var left = [] | |
var both = [] | |
var right = [] | |
var i = 0 | |
var j = 0 | |
while (i < a.length && j < b.length) { | |
if (a[i] < b[j]) { | |
left.push(a[i]) | |
++i | |
} else if (b[j] < a[i]) { | |
right.push(b[j]) | |
++j | |
} else { | |
both.push(a[i]) | |
++i | |
++j | |
} | |
} | |
while (i < a.length) { | |
left.push(a[i]) | |
++i | |
} | |
while (j < b.length) { | |
right.push(b[j]) | |
++j | |
} | |
return { left: left, both: both, right: right } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment