-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7kyu-allUnique.js
22 lines (18 loc) · 994 Bytes
/
7kyu-allUnique.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
Write a program to determine if a string contains only unique characters. Return true if it does and false otherwise.
The string may contain any of the 128 ASCII characters. Characters are case-sensitive, e.g. 'a' and 'A' are considered different characters.
*/
//P:one input, a string that may contain any of the 128 ascii chars
//R: return a boolean value to determine if the string contains only unique chars
//E: ' nAa' => false, contains 2 spaces
// 'abcdef' => true
// 'aA' => true, lc and uc chars are different in ascii
//P: iterate over string, pushing ascii char codes to a char code array
// filter the array to remove any dupes
// return boolean value for if filtered array length matches unfiltered array length
function hasUniqueChars(str){
let chars = str.split('');
let codes = chars.map(el => el.charCodeAt(el));
let noDupeCodes = codes.filter(el => codes.indexOf(el) === codes.lastIndexOf(el));
return codes.length === noDupeCodes.length;
}