0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

単語のカウントFINAL問題 Map.keys()でシンプルに!

Posted at

これまで解いてきたことを活かして、単語のカウント最終問題を解いたから解説するよ!


📌 問題概要

与えられた半角スペース区切りの単語列から、
登場順に単語とその出現回数を出力する。


💡 入力例

red green blue blue green blue

✅ 出力例

red 1
green 2
blue 3


💥 初めの実装(標準入力省略)

前回のコードをつかって

const words = input.split(" ");
const dict = new Map();
const order = []; // 単語の出現順を記録

words.forEach(word => {
    if (!dict.has(word)) {
        dict.set(word, 0);
        order.push(word);
    }
    dict.set(word, dict.get(word) + 1);
});

// 出現順に単語とカウントを出力
order.forEach(word => console.log(word, dict.get(word)));


✨ 改善版コード

🚀 改善ポイント

✅カウントの初期化を set(word, 0) せずに set(word, 1) にする
  初回の set 時に 0 にして、すぐに +1 するのは無駄
  set(word, 1) にすれば +1 の処理が不要になる

order を配列ではなく Map.keys() を使う
 order 配列を使わなくても Map.keys() でキーの順番を保持できる
 Map はキーを挿入順に保持する特性があるので order は不要

const words = input.split(" ");
const dict = new Map();

words.forEach(word => {
    dict.set(word, (dict.get(word) || 0) + 1);
});

// 挿入順に単語と出現回数を出力
for (const word of dict.keys()) {
    console.log(word, dict.get(word));
}


🛠 改善点の説明

✅カウントの初期化を (dict.get(word) || 0) + 1 で処理
 dict.get(word) || 0 により undefined の場合は 0 にする
 set(word, (dict.get(word) || 0) + 1) にすれば if 文不要

order を削除し、dict.keys() を使う
 order を作らなくても Map.keys() で挿入順にアクセス可能

forEach ではなく for…of を使う


💡 まとめ

Map の特性を活かせば、余計な変数を減らせてコードがスッキリ! 「データ構造の理解 = コードの最適化」 だと実感した。



🔗 僕の失敗談と解決話!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?