0
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 1 year has passed since last update.

【Swift】配列同士の引き算

Posted at

実装

配列を比較して、重複する要素を削除する方法です。
平たく言うと、配列同士の引き算をして差分を求める方法です。

例1
let intArray: [Int] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] //引かれる
let evenArray: [Int] = [0, 2, 4, 6, 8] //引く

let oddArray: [Int] = intArray.filter{ !evenArray.contains($0) }

print(oddArray)
// -> [1, 3, 5, 7, 9]
例2
let words: [String] = ["しりとり", "りんご", "ごりら", "ラッパ", "パンダ"] //引かれる
let animals: [String] = ["ごりら", "パンダ"] //引く

let nonAnimals: [String] = words.filter{ !animals.contains($0) }

print(nonAnimals)
// -> ["しりとり", "りんご", "ラッパ"]

参考

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