By including a function to sort by I'm no longer doing an alphabetical sort, which means this problem no longer exists.

console.log([2,3,4,11].sort((a,b)=>a-b));

Output:
(4)Β [2, 3, 4, 11]

The sort() method sorts the elements of an array in place and returns the array. The default sort order is built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

defclosestNumber(x:Int,y:Int,givenNumber:Int):Int={if(abs(givenNumber-x)>abs(givenNumber-y)){y}else{x}}valgivenNumber=900valnums=List(100,200,400,800,1600,3200,6400,128000)println(nums.reduce((x,y)=>closestNumber(x,y,givenNumber)))//==> Prints 800

Javascript:Let's go for a JS one liner π

This causes the original array to have its order changed but I don't think that's against the rules π

I dont think use

`sort`

is good idea:Let see:

By including a function to sort by I'm no longer doing an alphabetical sort, which means this problem no longer exists.

developer.mozilla.org/en-US/docs/W...

yes, by including

compareFunctioncallback, we can solve this problem.If it outputs the closest number, it works! :)

Window functions!

hahaha thinking outside the box :D

I looked at it again just now and the

`row_number`

is redundant anyway...This is a job for Reduce!

JavaScript:

Ruby:

Shamelessly taken from Stack Overflow π I was in a much more of a "just give me the answer" mood than "let's figure it out" mood.

Here goes Python !

Java:

Trying to optimize no of lines with Java 8 Streams and Lambda.

A Python implementation:

Using JavaScript

`Set`

:Or we can save the intermediate array:

And there is another perspective, instead of iterating numbers, we can iterate distances. Like:

This will normally be slower, but in cases like:

`nums = [10000000, 9999999, 9999998, ..., 1]`

`given_num = 10000001`

It will be much faster than other algorithms.

I have written a benchmark function for this:

You can test your function via

`benchmark(func)`

.The solution above yields:

`closest_num: 5037.456787109375ms`

in Chrome.

Hey! Let's do it the Scala way:

Thanks for the challenge!

With the help of google, I was able to find the answer to this π

import numpy as np

def find_nearest(array, value):

array = np.array(array)

z=np.abs(array-value)

y= np.where(z == z.min())

m=np.array(y)

x=m[0,0]

y=m[1,0]

near_value=array[x,y]

array =np.array([[100, 200, 400, 800, 1600, 3200, 6400, 128000]])

print(array)

value = 900

print(find_nearest(array, value))

## Answer:

Thanks for this challenge! πΊ

OCaml with list instead of array

nums = [100, 200, 400, 800, 1600, 3200, 6400, 128000];

given_num = 900

Here is my solution:

Usage:

Result:

Be careful that

`...`

can cause a range error:`RangeError: Maximum call stack size exceeded`

when the

`nums`

array is very large.Here's some C# for everyone:

Perl solution:

play.golang.org/p/xxzN4y-fPF0

C++, O(n), no sorting, no extra memory allocation beyond storing the input:

Rust π¦

Nice, didn't know Rust had similar syntax to Ruby!

Wolfram Language!

I knew that one day my subscription will come in handy...

What if there are two numbers that are equally close to the given number?

I think you should point this out in the description.

I probably over complicated it, but here's a CodePen link with how I did it in JS and outputted it to the HTML side.

Long as you solved it! π

If the given array is ascending/descending, then the task becomes a binary search.

The pleasure of programming lies in that even those things appearing to be very simple are also worth thinking.

yeah, that's why I used a binary search library function in my answer :)

JS:

const nums = [100, 200, 400, 800, 1600, 3200, 6400, 128000];

const givenNum = 900;

nums

.map(n => ({n, d: Math.abs(n-givenNum)}))

.sort((n1, n2) => Math.sign(n1.d - n2.d))[0].n