I am curious if there is a way in swift to achieve the most closest value via their modern api's?
For example:
let x = [1.2, 3.4, 4.5, 6.7, 8.9]
print(x.getClosestValue(3.7) //3.4
I have been playing around with map and reduce but still not be able to crack this problem. The problem occur's that I have to iterate through entire array to detect false positives as well. There can be a scenario where you can have multiple closest values, so was just wondering how this can be done in swiftly way?
There is no straight api from Apple to use yet. If you don't care about the time, you can sort the array and use Array's
first(where:)orlast(where:)methods to do linear search. However, you can make it better by sort and using binary search, it will probably just take you additional 20 lines?