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?

外部ライブラリを利用せずにVecをDistinctしたい

Last updated at Posted at 2024-05-01

Rustで外部ライブラリを利用せず、VecをDistinctする方法を紹介します。

まずVecをHashSetに詰め替え、再度Vecに変換する方法があります。

use std::collections::HashSet;

let a = vec![3, 2, 2, 3, 1, 3];
let b = a.into_iter().collect::<HashSet<_>>().into_iter().collect::<Vec<_>>();
println!("{:?}", b); //=> [2, 3, 1]

dedupを利用する方法もあります。

let mut a = vec![3, 2, 2, 3, 1, 3];
a.sort();
a.dedup();
println!("{:?}", a); //=> [1, 2, 3]

環境情報

$ cargo --version
cargo 1.76.0 (c84b36747 2024-01-18)
$ rustc -V
rustc 1.76.0 (07dca489a 2024-02-04)
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?