1
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?

More than 1 year has passed since last update.

paizaラーニング レベルアップ問題集 ソートメニュー応用編 JavaScript 重複削除

Last updated at Posted at 2022-09-12

重複削除 (paizaランク C 相当)

集合Setで重複削除をします。スプレッド構文で配列に変換します。

解答コード例

JavaScript
const fs = require("fs");
const input = fs.readFileSync("/dev/stdin", "utf-8").trim();
const lines = input.split("\n");
//数列の長さを表す整数 n 
const [n, k] = lines[0].split(" ").map(Number);
//数列 a
const a = lines[1].split(" ").map(Number);
//数列 a から重複した要素をすべて削除
let set = new Set([...a]);//集合Setで重複削除
//残った要素を昇順かつ半角スペース区切りで出力
console.log([...set].sort((a, b) => a - b).join(" "));//スプレッド構文で配列に変換
1
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
1
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?