Swift Array Programming Exercises: Check if a given array of integers contains 3 twice, or 5 twice
Write a Swift program to check if a given array of integers contains 3 twice, or 5 twice.
Pictorial Presentation:
Sample Solution:
Swift Code:
func double_35(_ a: [Int]) -> Bool {
if a == [3, 3] || a == [5, 5]
{
return true
} else {
return false
}
}
print(double_35([3, 3]))
print(double_35([5, 5]))
print(double_35([1, 2]))
Sample Output:
true true false
Swift Programming Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a Swift program to create a new array with double the lenght of a given array of integers and its last element is the same as the given array. The given array will be length 1 or more. By default, a new integer array contains all 0's.
Next: Write a Swift program to check if two given arrays of integers have 0 as their first element.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics