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?

平均以上の数を列挙 reduce()とfilter()

Posted at

Paizaの「平均以上の値を出力する問題」に挑戦したら、配列メソッドの使い分けがめっちゃ学びになったので、まとめる!

📝 問題内容

1行目に整数 N
2行目に N 個の整数が与えられる。

この N 個の整数のうち、平均以上の値をすべて出力するという問題

入力例:

5
1 2 3 4 5

出力例る:

3
4
5



✅ 解法その1:forEachで1つずつチェック

const rl = require('readline').createInterface({ input: process.stdin });
const lines = [];

rl.on('line', (input) => {
    lines.push(input);
});


rl.on('close', () => {
    const N = Number(lines[0]);
    const nums = lines[1].split(' ').map(Number);

    let total = 0;
    nums.forEach(num => total += num);

    const average = total / N;

    nums.forEach(num => {
        if (num >= average) {
            console.log(num);
        }
    });

});

解説:

  • forEach() で2回ループを回している

    • 1回目で合計を出し、
    • 2回目で平均以上の値をチェックして出力。
  • 直感的にわかりやすくて、初心者にもおすすめの書き方!


✅ 解法その2:filter()+forEach()でスリムに

const rl = require('readline').createInterface({ input: process.stdin });
const lines = [];

rl.on('line', (input) => {
    lines.push(input);
});


rl.on('close', () => {
    const nums = lines[1].split(' ').map(Number);
    const average = nums.reduce((sum, num) => sum + num, 0) / nums.length;

    nums
        .filter(num => num >= average)
        .forEach(num => console.log(num));

});

解説:

  • reduce() で一発で合計を出して平均を計算。
  • filter() で平均以上の要素だけを抽出。
  • そのまま forEach() で順番通りに出力。



🎓 新しく学んだことまとめ

reduce():

  • 配列の要素を累積的に処理して、1つの値にまとめる。
  • 合計や積など、累積系処理に大活躍!

書き方↓

array.reduce((acc, cur) => acc + cur, 初期値);

filter():

  • 条件に合う要素だけを残して、新しい配列を作る。




僕の失敗談(´;ω;`)と解決法🐈

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?