Skip to content

Instantly share code, notes, and snippets.

@timetocode
Created October 1, 2015 00:15
Show Gist options
  • Save timetocode/d7ef265234f622a9a0fb to your computer and use it in GitHub Desktop.
Save timetocode/d7ef265234f622a9a0fb to your computer and use it in GitHub Desktop.
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