-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisUnique.js
More file actions
43 lines (40 loc) · 969 Bytes
/
isUnique.js
File metadata and controls
43 lines (40 loc) · 969 Bytes
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
// Функция проверки уникальности всех символов в строке:
// Classic
function isUnique(string) {
let obj = {};
for (let i = 0; i < string.length; i++) {
if (obj[string[i]]) {
obj[string[i]]++;
} else {
obj[string[i]] = 1;
}
}
if (Object.values(obj).every((item) => item === 1)) {
return true;
}
return false;
}
// ES6
const isUnique = (string) => {
for (let i = 0; i < string.length; i++) {
if (string.indexOf(string[i]) !== i) {
return false;
}
}
return true;
};
// Advanced
function isUnique(string) {
let setObj = new Set();
for (let i = 0; i < string.length; i++) {
if (setObj.has(string[i])) {
return false;
}
setObj.add(string[i]);
}
return true;
}
console.log(isUnique("abcdef")); // -> true
console.log(isUnique("1234567")); // -> true
console.log(isUnique("abcABC")); // -> true
console.log(isUnique("abcadefa")); // -> false