Skip to content

Instantly share code, notes, and snippets.

@Hashbrown777
Last active June 8, 2023 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hashbrown777/96fc6a0b998dfe42eea3a3cfd767aef9 to your computer and use it in GitHub Desktop.
Save Hashbrown777/96fc6a0b998dfe42eea3a3cfd767aef9 to your computer and use it in GitHub Desktop.
//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);
}
}
}
//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