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.

Swift 配列内の特定の値がいくつあるか調べる

Posted at

#配列内の特定の値がいくつあるか数える


//配列の作成
var array = [9,8,7,6,7]

//カウントするインスタンス
var count = 0

//配列から要素を取り出す
for num in array {
  if num == 7 { //7ならばカウントを1増やす
    count = count + 1
    print("\(num)がありました")
  }
}

//確認し終わったら、何個あったか調べる

if count > 0 {
  print("7は\(count)個ありました")
  
} else {
  print("7がありませんでした")
}
1
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
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?