-
Notifications
You must be signed in to change notification settings - Fork 0
/
가장많은글자 백준.js
47 lines (32 loc) · 1.1 KB
/
가장많은글자 백준.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
const findDistinct = (searchChar,letters) => {
const text = letters;
const resultObj = {};
let count = 0;
let pos = text.indexOf(searchChar); //pos는 0의 값을 가집니다.
while (pos !== -1) {
count++;
pos = text.indexOf(searchChar, pos + 1); // 첫 번째 a 이후의 인덱스부터 a를 찾습니다.
}
resultObj.char = searchChar;
resultObj.count = count;
return resultObj;
}
const solutions = () => {
const resultArr =[];
let letters = 'abcab';
letters = letters.toUpperCase();
for(let i = 0 ; i < letters.length ; i++){
const resultObj = {
char : findDistinct(letters[i],letters).char,
count : findDistinct(letters[i],letters).count
}
resultArr.push(resultObj);
}
let uniqueObjArray = [
...new Map(resultArr.map((item) => [item["char"], item])).values(),];
const maxValueOfArr = Math.max(...uniqueObjArray.map(o => o.count), 0);
const filteringArr = uniqueObjArray.filter(item=> item.count === maxValueOfArr);
const result = filteringArr.map(item=> item.char.toLowerCase())
console.log(result.join(""))
}
solutions();