Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

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

//配列の作成
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がありませんでした")
}
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away