Skip to content

Instantly share code, notes, and snippets.

@Andaeiii
Last active April 1, 2022 15:41
Show Gist options
  • Save Andaeiii/13e508c13e620e4060ff09c35d978f63 to your computer and use it in GitHub Desktop.
Save Andaeiii/13e508c13e620e4060ff09c35d978f63 to your computer and use it in GitHub Desktop.
From a given array of numbers find the lonely integer
function lonelyinteger(a) {
// Write your code here
let isUnique = (value, index, self) => self.indexOf(value) === index;
let distinct = a.filter(isUnique);
let lonelyint = null;
distinct.forEach(v => {
if (a.filter(index => index == v).length == 1) {
lonelyint = v;
}
});
return lonelyint;
}
//usage
let arr = [1, 2, 3, 4, 5, 6, 72, 1, 6, 5, 5, 4, 3];
console.log(lonelyinteger(arr));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment