๋ฐ์ํ
๋ด ํ์ด (์ ๋ต)
const fs = require('fs');
let input = (fs.readFileSync('./dev/stdin') + '').toString().trim().split('\n');
input.shift();
let bookDict = {};
input.map((book) => {
if (!bookDict[book]) bookDict[book] = 1;
else bookDict[book] += 1;
});
const sortBook = Object.entries(bookDict)
.sort(([aa, a], [bb, b]) => {
if (a === b) {
return aa < bb ? -1 : aa == bb ? 0 : 1;
}
return b - a;
})
.reduce((r, [k, v]) => ({ ...r, [k]: v }), {});
console.log(Object.keys(sortBook)[0]);
1. book ๋์ ๋๋ฆฌ ์๋ฃ๊ตฌ์กฐ ์์ฑ
2. ์ ๋ ฅ ๋ฐ์ ์ฑ ๋ฆฌ์คํธ๋ฅผ ๋๋ฉฐ dictonary ์ key๊ฐ์ผ๋ก ์ฑ์
3. ๋ค์ค์กฐ๊ฑด ์ ๋ ฌ ( 1. value๊ฐ 2. ์ํ๋ฒณ๊ฐ)
4. ์ ๋ ฌํ sortBook์ ์ฒซ๋ฒ์งธ key ๊ฐ ๋์ถ
Object.entries
๊ฐ์ฒด ์์ฒด์ [key, value] ์์ ๋ฐฐ์ด ๋ฐํ
๋ค๋ฅธ ํ์ด
const result = Object.entries(obj).sort((a,b) => {
if(a[1] === b[1]){
return a[0] > b[0] ? 1: -1
}
return b[1] > a[1] ? 1: -1
})
key ์ value ๋๊ฐ์ง ๋ค ์ฌ์ฉํ ๊ฒฝ์ฐ์ ๊ตณ์ด [aa,a]๋ก ๋ํ๋ผ ํ์ ์์ด a[0] a[1] ๋ก ํํ์ด ๊ฐ๋ฅํ๋ค๋ ๊ฒ์ ์๊ฒ ๋์๋ค.
JavaScript์์ value ๊ฐ์ผ๋ก ์ ๋ ฌํ๋ ๋ฐฉ๋ฒ์ ๐ ๊ธฐ์ต๋ณด๋ค๊ธฐ๋ก์์ ์ฐธ๊ณ ํ์ต๋๋ค..
๐ฅณ ์ค๋์ ํ๋ฐฉ์ ๋ง์๋ค~
'๐ ์๊ณ ๋ฆฌ์ฆ > ๋ฐฑ์ค JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] node.js/ 15650 N๊ณผM(2) (0) | 2021.10.21 |
---|---|
[๋ฐฑ์ค] node.js/ 15649 N๊ณผ M 1 (0) | 2021.10.20 |
[๋ฐฑ์ค] node.js/ 10866 ๋ฑ (0) | 2021.10.18 |
[๋ฐฑ์ค] node.js/ 10828 ์คํ (0) | 2021.10.15 |
[๋ฐฑ์ค] node.js/ 3986 ์ข์ ๋จ์ด (0) | 2021.10.14 |
๋๊ธ