Swift Array Programming Exercises: Test if the number of 1's is greater than the number of 3's of a given array of integers
Swift Array Programming: Exercise-28 with Solution
Write a Swift program to test if the number of 1's is greater than the number of 3's of a given array of integers.
Pictorial Presentation:
Sample Solution:
Swift Code:
func test_nums(array_nums: [Int]) -> Bool {
var p = 0
var q = 0
for x in 0..<array_nums.count {
if array_nums[x] == 1 {
p += 1
}
else if array_nums[x] == 3 {
q += 1
}
}
return p > q
}
print(test_nums(array_nums: [1, 3, 1]))
print(test_nums(array_nums: [1, 3, 1, 3, 2]))
print(test_nums(array_nums: [3, 3]))
Sample Output:
true false false
Swift Programming Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a Swift program to check if a given array of integers contains a 3 next to a 3 somewhere.
Next: Write a Swift program to test if every element is a 2 or a 5 of a given array of integers.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/swift-programming-exercises/array/swift-array-exercise-28.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics