65
52

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.

【Swift】配列内で重複する要素を除去する

Last updated at Posted at 2015-03-14

NSOrderedSet

NSOrderedSetは「重複した要素を保存しない」という特徴を持つ。

Array -> NSOrderdSet -> Array

ArrayからNSOrderdSetを生成し、それをまたArrayに戻すと、重複した要素が除去される。
各要素の順番は、元のまま保たれる。

サンプル

/* Swift2.1 */

let values = ["1st", "2nd", "3rd", "2nd"]
let orderedSet = NSOrderedSet(array: values)
let uniqueValues = orderedSet.array as! [String]

print(uniqueValues) // ["1st", "2nd", "3rd"]

参考

65
52
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
65
52

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?