max char

Problem

๋ฌธ์ž์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ€์žฅ ๋งŽ์ด ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

Solution

  1. ๊ฐ์ฒด๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ๊ฐ€์žฅ ๋งŽ์ด ๋ฐ˜๋ณต๋œ ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

function maxChar() {
 const charMap = {}
    let max = 0
    let maxChar = ''

    for(let char of str) {
        if(charMap[char]) {
            charMap[char]++
        } else {
            charMap[char] = 1
        }
    }

    for (let char in charMap) {
        if(charMap[char] > max) {
            max = charMap[char]
            maxChar = char
        }
    }

    return maxChar
}

Discussion

๊ฐ์ฒด๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ๊ฐ€์žฅ ๋งŽ์ด ๋ฐ˜๋ณต๋œ ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๊ฐ์ฒด์— ๋ฌธ์ž์—ด์˜ ๊ฐ ๋ฌธ์ž๋ฅผ key๋กœ, ๊ฐ ๋ฌธ์ž์˜ ๋ฐ˜๋ณต ํšŸ์ˆ˜๋ฅผ value๋กœ ์ €์žฅํ•œ๋‹ค.

Last updated