1
1

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 5 years have passed since last update.

配列を配列でFilterする。

Posted at

#配列を配列でフィルタする。
二つの配列があり、元の配列とフィルタ用の配列両方に含まれていれば新しい配列に追加する。
[1,2,3,3,4,5] [3,4,5,6] の二つがあれば [3,3,4,5]が出力される。
filterを使うと思いの外短く書けたのでメモ
*ソートされている必要はない。


//元の配列
let Nums = [1,2,3,3,4,5]
//フィルタ用配列
let Filter = [3,4,5,6]

//フィルタする
let Result = Nums.filter({(a: Int) -> Bool in return Filter.contains(a) })

print(Result) // [3, 3, 4, 5]

1
1
2

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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?