-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallUnique.js
55 lines (45 loc) · 1.21 KB
/
allUnique.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Is Unique: Implement an algorithm to determine if a string has all unique
// characters.What if you cannot use additional data structures ?
function isUniqueWord(word) {
const letterMap = new Map();
for (let letter of word) {
if (letterMap.has(letter)) {
return false;
} else {
letterMap.set(letter, 1);
}
}
return true;
}
// not unique; 'hotpotato'
// look at each letter in word and add to hash map
// if letter already exists in map then return early
function isUnique(word) {
const letterMap = new Map();
for (letter of word) {
if (letterMap.has(letter)) {
return false;
} else {
letterMap.set(letter, 1);
}
}
return true;
}
// console.log(isUnique("moonica"));
//console.log(isUnique("abc"));
function isUniqueTwo(word) {
// sort letters in array - if the next item is the same letter than return false
word.split("").sort((a, b) => a - b);
let i;
for (i = 0; i < word.length; i++) {
if (word[i] == word[i + 1]) {
return false;
}
}
return true;
}
console.log(isUniqueWord("moonica"));
console.log(isUniqueWord("monica"));
console.log(isUniqueWord("abc"));
console.log(isUniqueTwo("moonica"));
console.log(isUniqueTwo("abc"));