
- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Data Types
- Swift - Variables
- Swift - Optionals
- Swift - Tuples
- Swift - Constants
- Swift - Literals
- Swift - Operators
- Swift - Decision Making
- Swift - Loops
- Swift - Strings
- Swift - Characters
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift - Functions
- Swift - Closures
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift - Initialization
- Swift - Deinitialization
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Type Casting
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
Swift Program to Count the elements of an Array
In this article, we will learn how to write a swift program to count the elements of an array. To count the elements of an array swift provide a count property. This property will return the total number of elements present in the specified array. For example −
Arr = [3, 5, 6, 4, 6] so the count will return 5.
Syntax
var count: Int{get}
Or you can also write it as −
arrayName.count
Here count is the property of the array to count the total elements present inside the array. To use this property we use the dot operator in between the array name and the property name.
Algorithm
Step 1 − Create an array of any data type.
Step 2 − Count the elements of the array using the count property and store the result in another variable.
var res1Array = IntArray.count
Step 3 − Print the output
Example
import Foundation import Glibc // Creating an array of integer type var IntArray : [Int] = [34, 56, 23, 55, 78, 12] // Creating an array of string type var StringArray : [String] = ["Cow", "Bird", "Monkey", "Cat"] // Counting the elements of the array // Using count property var res1Array = IntArray.count var res2Array = StringArray.count print("Integer Array:", IntArray) print("Total number of elements:", res1Array) print("\nString Array:", StringArray) print("Total number of elements:", res2Array)
Output
Integer Array: [34, 56, 23, 55, 78, 12] Total number of elements: 6 String Array: ["Cow", "Bird", "Monkey", "Cat"] Total number of elements: 4
Here in the above code, we create two different types(that is Int and String) of arrays and then count their elements using the count property and display the final result.
Conclusion
So, this is how we can count the total number of elements present in the array with the help of the count property.
- Related Articles
- Swift Program to Shuffle the Elements of an Array
- Swift Program to Rotate Elements of an Array
- Swift Program to Sort the Elements of an Array in Ascending Order
- Swift Program to Sort the Elements of an Array in Descending Order
- Swift Program to Remove Duplicate Elements From an Array
- Swift Program to fetch elements from an array based on an index
- Swift Program to Find Common Array Elements
- Swift Program to get array elements after N elements
- Swift Program to Copy All the Elements of One Array to Another Array
- Swift Program to Remove All the Elements from the Array
- Swift Program to Print an Array
- Swift Program to Calculate the sum of Elements in a Given Array
- Swift Program to Count Number of Digits in an Integer
- Swift Program to Iterate Over an Array
- Swift Program to Find Mean of an Unsorted Array
