I want to iterate through a multidimensional array and compare the elements with one another and increment a value if a condition is met
var arrayExample = [[4,8],[15,30],[25,50]];
var noOfSimiRect: Int = 0
arrayExample.reduce(0) {
let a = $0[0] //error: Value of type 'int' has no subscripts
let b = $0[1] //error: Value of type 'int' has no subscripts
let c = $1[0]
let d = $1[1]
if a.isMultiple(of: c) && b.isMultiple(of: d) {
noOfSimiRect += 1
}
}
print(noOfSimiRect)
Alternatively, if I used the following syntax, I am still getting the error there
var arrayExample = [[4,8],[15,30],[25,50]];
var noOfSimiRect: Int = 0
arrayExample.reduce(0) {
let a = $0.0 //error: value of type 'int' has no member '0'
let b = $1.0 //error: value of type 'int' has no member '1'
let c = $0.0 //error: value of type '[int]' has no member 0'
let d = $1.0 //error: value of type '[int]' has no member '1'
if a.isMultiple(of: c) && b.isMultiple(of: d) {
noOfSimiRect += 1
}
}
print(noOfSimiRect)
Thank you to David and Martin for answering my question but I found a catch in both your solutions.
var arrayExample = [[4,8],[15,30],[25,50]];
In the given array I want the following:
- compare [4,8] with [15,30] and [25,50]
- compare [15,30] with [4,8] and [25,50]
- compare [25,50] with [4,8] and [15,30]