Last active
August 28, 2018 07:44
-
-
Save lienista/9d2181a1b5da81823221137126a9d6e4 to your computer and use it in GitHub Desktop.
Algorithms in Javascript: CTCI 1.1 - Is Unique: Write a function to determine if a string has all unique characters.
This file contains hidden or 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
| const stringHasUniqueChars = (str) => { | |
| str.split('').sort().join(''); | |
| for(let i=0;i<str.length; i++) { | |
| if(str[i] === str[i+1]) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| let s = 'Geeks for Geeks'; | |
| let y = stringHasUniqueChars(s); | |
| let x = 'abc'; | |
| let z = stringHasUniqueChars(x); | |
| console.log(s + ': ' + y); | |
| console.log(x + ': ' + z); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment