Last active
June 8, 2023 15:33
-
-
Save Hashbrown777/96fc6a0b998dfe42eea3a3cfd767aef9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//leaves only the unique elements in two sorted arrays | |
function diff(bob, steve) { | |
for (let b = 0, s = 0; b < bob.length && s < steve.length; ++b, ++s) { | |
if (bob[b] < steve[s]) | |
--s; | |
else if (bob[b] > steve[s]) | |
--b; | |
else { | |
bob.splice(b--, 1); | |
steve.splice(s--, 1); | |
} | |
} | |
} |
This file contains hidden or 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
//gets the index an element would belong in a sorted array | |
function ( | |
arr, | |
el, | |
prefer = (prefer, against) => ( | |
(prefer == against) ? null : prefer < against | |
) | |
) { | |
let index = 0; | |
for ( | |
let top = arr.length, preference, test; | |
index < top; | |
(preference !== false) && (top = test), | |
(preference !== true) && (index = test + 1) | |
) | |
preference = prefer(el, arr[test = (index + top) >>> 1]); | |
return index; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment