'Swift: Check which value in NSArray is closest to another given value
Lets say I have an array
var values:[CGFloat] = [-12.0, 450, 300]
I need to find out which of these numbers is closest to a given value, say
var givenValue:CGFloat = 64
Is there an efficient way to find out which object in the array is closest to 64?
I know you can do something like this:
if abs(values[0] - 64) < abs(values[1] - 64) && abs(values[0] - 64) < abs(values[2] - 64) {
println("values[0] is the closest to 64)
}
But this will result in several if-statements and seems inefficient.
Does anyone know a better way to do this? In this example I would need the value in the array as well as which objectIndex in the array it is.
Solution 1:[1]
Save the minimumDifference
as a variable.
Then iterate the array. Each time compare the difference in the value from the array to the minimum difference.
If the new difference is smaller then swap out the minimu difference.
At the end of the array you will have the minimum difference.
This is the same as finding the highest value, smallest value, etc...
Solution 2:[2]
In Swift 4.2 this can be done using array first(where:)
and last(where:)
methods. Be aware that the example code below has unsafe dererencing of optional values and will fail if givenValue
is outside the range of the values
array.
func findClosest(_ values: [CGFloat], _ givenValue: CGFloat) -> CGFloat {
let sorted = values.sorted()
let over = sorted.first(where: { $0 >= givenValue })!
let under = sorted.last(where: { $0 <= givenValue })!
let diffOver = over - givenValue
let diffUnder = givenValue - under
return (diffOver < diffUnder) ? over : under
}
let values:[CGFloat] = [-12.0, 450, 300]
print(findClosest(values, 64.0)) // -12.0
print(findClosest(values, 143.0)) // -12.0
print(findClosest(values, 144.5)) // 300
Solution 3:[3]
For completion's sake, I will post my final code that solved this
//Array to hold dist. of visible cell to pt. 64
var distancesToTop = [CGFloat]()
//Array of visible cell indexPaths
var indexPaths = tableView.indexPathsForVisibleRows()!
for visibleCell in tableView.visibleCells() { //for each visible cell...
//Append distance to 64 to the array
distancesToTop.append(abs((visibleCell.frame.minY - tableView.contentOffset.y) - 64))
}
//Find the lowest of those values
let numMin = distancesToTop.reduce(CGFloat.max, { min($0, $1) })
//Determine the objectForIndexPath that the minimum number was in
let num = find(distancesToTop, numMin)!
Solution 4:[4]
You can use tuples in order to save the nearest value and the distance. This code sets the nearest value to 0, even the given value is not in the range it will return 0. if you want you can make the value as optional then wrap it.
func findClosest(_ values: [CGFloat], _ givenValue: CGFloat) -> CGFloat {
var result: (nearest: CGFloat, distance: CGFloat) = (0, .greatestFiniteMagnitude)
for point in values.sorted(){
let distance = abs(point - givenValue)
if result.distance > distance {
result = (nearest: point, distance: distance)
}
}
return result.nearest
}
You can also use AdditiveArithmetic in order to make the function more generic for numbers.
Solution 5:[5]
I would use reduce on the array to find the closest value. This would require only one if-else
statement and appear to be clean. I don’t like that the return value is nil
; however, I try to avoid throwing
errors and returning the given value would be inaccurate.
var values: [CGFloat] = [100, 150, 200]
let pivot: CGFloat = 120
let closest = getClosestValue(to: pivot, from: values) /// 100
func getClosestValue(to pivot: CGFloat, from values: [CGFloat]) -> CGFloat? {
guard let firstValue = values.first else { return nil }
return values.reduce(firstValue) { partialResult, nextValue in
let partial = abs(pivot - partialResult)
let next = abs(pivot - nextValue)
if partial > next {
return nextValue
} else {
return partialResult
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Fogmeister |
Solution 2 | |
Solution 3 | George Poulos |
Solution 4 | Mehmet Baykar |
Solution 5 |