Last active
April 1, 2022 15:41
-
-
Save Andaeiii/13e508c13e620e4060ff09c35d978f63 to your computer and use it in GitHub Desktop.
From a given array of numbers find the lonely integer
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 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