1

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")}
Dan.code
  • 431
  • 2
  • 14
  • 2
    this is not Swift. post your actual code attempt. If y were just a Double (not an array) you could use contains(where:) `if x.contains(where: { $0 > y }) { print(true) }` – Leo Dabus Jun 18 '17 at 22:41

2 Answers2

3

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)")
rmaddy
  • 314,917
  • 42
  • 532
  • 579
0
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]
Bobby
  • 6,115
  • 4
  • 35
  • 36