Is there any such statement to do something similar to below? or do I need to create a function?
let x=[Double](1.023, 2.023, 3.023, 4.023, 5.023)
ler y=[Double](3.001)
if any of x > y{
("YES")}
Is there any such statement to do something similar to below? or do I need to create a function?
let x=[Double](1.023, 2.023, 3.023, 4.023, 5.023)
ler y=[Double](3.001)
if any of x > y{
("YES")}
You can use the contains(where:)
method of Array.
let x = [1.023, 2.023, 3.023, 4.023, 5.023]
let y = 3.001
if x.contains(where: { $0 > y }) {
print("YES")
}
If you want to know the first value that was greater, you can do:
let x = [1.023, 2.023, 3.023, 4.023, 5.023]
let y = 3.001
if let firstLarger = x.first(where: { $0 > y }) {
print("Found \(firstLarger)")
}
If you want to know all that are larger, you can use filter
.
let x = [1.023, 2.023, 3.023, 4.023, 5.023]
let y = 3.001
let matches = x.filter { $0 > y }
print("The following are greater: \(matches)")
let x = [1.023, 2.023, 3.023, 4.023, 5.023]
let y = [3.001]
let result = x.filter { $0 > y[0] }
print(result) // [3.023, 4.023, 5.023]