Skip to content

Instantly share code, notes, and snippets.

@Lavrend
Created May 16, 2019 20:54
Show Gist options
  • Save Lavrend/ddc82d3c903f1b723614896699fec2e7 to your computer and use it in GitHub Desktop.
Save Lavrend/ddc82d3c903f1b723614896699fec2e7 to your computer and use it in GitHub Desktop.
Checks if words are the same letter
const arrSame = ['va5ya', '5avya', '5ayva'];
const arrDiff = ['cat', 'act', 'car'];
const isUniqChars = (arr) => {
const sortFn = (a, b) => {
if (a > b) return 1;
if (b > a) return -1;
return 0;
};
const getUniqVal = (value, index, self) => {
return self.indexOf(value) === index;
};
const sorted = arr.map(item => item.split('').sort(sortFn).join(''));
return (sorted.filter(getUniqVal)).length <= 1;
};
console.log(isUniqChars(arrSame)); // true
console.log(isUniqChars(arrDiff)); // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment