728x90
const input = require('fs').readFileSync('/dev/stdin').toString().trim().replaceAll("\r" && "\n", "").toUpperCase().split("");
let countArr = [];
const inputSet = new Set(input);
const onlyItem = [...inputSet];
onlyItem.map(v => {
let count = 0;
for (let i = 0; i < input.length; i++) {
v === input[i] && count++;
}
countArr.push({ [v]: count });
})
let num = [];
for (let i = 0; i < countArr.length; i++) {
num.push(...Object.values(countArr[i]));
} //num [ 1, 4, 4, 1 ]
const max = Math.max(...num);
let maxcount = 0;
num.map(v => v === max && maxcount++); //max๊ฐ 1๊ฐ ์ด์์ธ์ง ํ์
ํ๊ธฐ
const findLetter = () => {
countArr.map(v => Object.values(v)[0] === max && console.log(...Object.keys(v)));
}
maxcount > 1 ? console.log('?') : findLetter();
input์ ๋ฌธ์๋ฅผ ๋ชจ๋ ๋๋ฌธ์๋ก ๋ฐ๊พผ ํ ๋ฐฐ์ด๋ก ๊ฐ์ ธ์๋ค.
Set์ผ๋ก ์ค๋ณต๋๋ ๋ฌธ์๋ฅผ ์์ ์คฌ๋ค.
๊ทธ๋ฆฌ๊ณ ํด๋น ๋ฌธ์๊ฐ ๋ช๊ฐ ์ง ์ธ์ฃผ๊ณ ๊ทธ ๊ฐ์ obj ํํ๋ก ๋ฐฐ์ด์ ๋ฃ์ด์ค.
obj๊ฐ ๋ค์ด์๋ ๋ฐฐ์ด์์ value ๊ฐ๋ง ๊ฐ์ ธ์จ ํ, max ๋ฅผ ๊ตฌํ๋ค.
๋ง์ฝ max ๊ฐ 2๊ฐ ์ด์์ด๋ผ๋ฉด ?์ ์ถ๋ ฅํด์คฌ๋ค.
max๊ฐ ํ๋๋ผ๋ฉด, max๋ฅผ ์ด์ฉํ key ๋ฅผ ๊ตฌํด์ค ํ ๊ทธ ๊ฐ์ ์ถ๋ ฅํ๋๋ก ํ๋ค.
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Object/values
728x90
๋ฐ์ํ
'ํ์ ๊ณต๋ถ > ๐ซง ์๊ณ ๋ฆฌ์ฆ ๊ณต๋ถ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค/Node.js] 1157๋ฒ. ๋ฐ๊ตฌ๋ ์์ ๋ฐ๊พธ๊ธฐ || ํ์ด JavaScript (0) | 2023.04.06 |
---|---|
[๋ฐฑ์ค/Node.js] 4344๋ฒ ํ๊ท ์ ๋๊ฒ ์ง || ํ์ด JavaScript (0) | 2023.04.05 |
[๋ฐฑ์ค/Node.js] 10757๋ฒ ํฐ์ A + B || ํ์ด JavaScript (0) | 2023.04.03 |
[๋ฐฑ์ค/Node.js] 2444๋ฒ ๋ณ์ฐ๊ธฐ || ํ์ด JavaScript (0) | 2023.04.02 |
[๋ฐฑ์ค/Node.js] 5622๋ฒ ๋ค์ด์ผ || ํ์ด JavaScript (0) | 2023.04.01 |