๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

ํ”„์—” ๊ณต๋ถ€/๐Ÿซง ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€

[๋ฐฑ์ค€/Node.js] 1157๋ฒˆ ๋‹จ์–ด ๊ณต๋ถ€ || ํ’€์ด JavaScript

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

 

Object.keys() - JavaScript | MDN

Object.keys() ๋ฉ”์„œ๋“œ๋Š” ์ฃผ์–ด์ง„ ๊ฐ์ฒด์˜ ์†์„ฑ ์ด๋ฆ„๋“ค์„ ์ผ๋ฐ˜์ ์ธ ๋ฐ˜๋ณต๋ฌธ๊ณผ ๋™์ผํ•œ ์ˆœ์„œ๋กœ ์ˆœํšŒ๋˜๋Š” ์—ด๊ฑฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

developer.mozilla.org

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Object/values

 

Object.values() - JavaScript | MDN

Object.values() ๋ฉ”์†Œ๋“œ๋Š” ์ „๋‹ฌ๋œ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง€๋Š” (์—ด๊ฑฐ ๊ฐ€๋Šฅํ•œ) ์†์„ฑ์˜ ๊ฐ’๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฐฐ์—ด์€ for...in ๊ตฌ๋ฌธ๊ณผ ๋™์ผํ•œ ์ˆœ์„œ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. (for in ๋ฐ˜๋ณต๋ฌธ์€ ํ”„๋กœํ† ํƒ€์ž…

developer.mozilla.org

 

728x90
๋ฐ˜์‘ํ˜•