Skip to content

Instantly share code, notes, and snippets.

@Lavrend
Created May 16, 2019 20:54
Show Gist options
  • Select an option

  • Save Lavrend/ddc82d3c903f1b723614896699fec2e7 to your computer and use it in GitHub Desktop.

Select an option

Save Lavrend/ddc82d3c903f1b723614896699fec2e7 to your computer and use it in GitHub Desktop.

Revisions

  1. Lavrend created this gist May 16, 2019.
    23 changes: 23 additions & 0 deletions uniq-chars.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    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