Skip to content

Instantly share code, notes, and snippets.

@hassan-maavan
Created September 19, 2020 06:52
Show Gist options
  • Save hassan-maavan/a5eacf35c638f78f995016b8ef9aa582 to your computer and use it in GitHub Desktop.
Save hassan-maavan/a5eacf35c638f78f995016b8ef9aa582 to your computer and use it in GitHub Desktop.
Take 2 strings s1 and s2 including only letters from ato z. Return a new sorted string, the longest possible, containing distinct letters, each taken only once - coming from s1 or s2.
Example:
// a = "xyaabbbccccdefww"
// b = "xxxxyyyyabklmopq"
// longest(a, b) -> "abcdefklmopqwxy"
// a = "abcdefghijklmnopqrstuvwxyz"
// longest(a, a) -> "abcdefghijklmnopqrstuvwxyz"
function longest(s1, s2) {
return (s1 + s2).split('').filter(onlyUnique).sort().join('')
}
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment