DEV Community

Andy Zhao (he/him)
Andy Zhao (he/him)

Posted on

Challenge: Get Closest Number in an Array

Given an array of numbers nums, and a given number given_num:

nums = [100, 200, 400, 800, 1600, 3200, 6400, 128000]
given_num = 900
Enter fullscreen mode Exit fullscreen mode

Get the number closest to the given_num from the array nums.

In this example, the returned value should be 800.

Go!

A black checkered flag signals go!

Oldest comments (36)

Collapse
 
andy profile image
Andy Zhao (he/him) • Edited

Ruby:

nums.min_by { |num| (given_num - num).abs }
#=> 800

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.

Collapse
 
dmfay profile image
Dian Fay

Window functions!

SELECT unnest
FROM unnest(ARRAY[100,200,400,800,1600,3200,6400,128000])
ORDER BY row_number() OVER (ORDER BY abs(900 - unnest))
LIMIT 1;
Collapse
 
rhymes profile image
rhymes

hahaha thinking outside the box :D

Collapse
 
dmfay profile image
Dian Fay

I looked at it again just now and the row_number is redundant anyway...

SELECT unnest
FROM unnest(ARRAY[100,200,400,800,1600,3200,6400,128000])
ORDER BY abs(900 - unnest)
LIMIT 1;
Collapse
 
maeganwilson_ profile image
Maegan Wilson

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.

Collapse
 
andy profile image
Andy Zhao (he/him)

Long as you solved it! πŸ‘

Collapse
 
hpj1992 profile image
Harshit • Edited

Java:

        int[] nums = {100, 200, 400, 800, 1600, 3200, 6400, 128000};
        int ans = 0;
        int given_num = 900;
        int minDistance = Integer.MAX_VALUE;
        for (int i =0; i < nums.length; i++) {
            int curDistance = Math.abs(nums[i] - given_num);
            if (curDistance < minDistance) {
                ans = nums[i];
                minDistance = curDistance;
            }
        }
        System.out.println(ans);

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

Collapse
 
andreyvladikin profile image
Andrey Vladikin

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

Collapse
 
kip13 profile image
kip

Rust πŸ¦€

numbers.iter().min_by_key(|&num| (num - given_num).abs()).unwrap()
Collapse
 
andy profile image
Andy Zhao (he/him)

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

Collapse
 
highcenburg profile image
Vicente G. Reyes • Edited

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]

return near_value

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! 🍺

Collapse
 
mkrl profile image
Mikhail Korolev

Wolfram Language!

Nearest[nums, given_num]
Enter fullscreen mode Exit fullscreen mode

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

Collapse
 
joelnet profile image
JavaScript Joel • Edited

This is a job for Reduce!

JavaScript:

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

const closestReducer = g => (a, b) =>
  Math.abs(g - a) < Math.abs(g - b) ? a : b

nums.reduce(closestReducer(given_num))
//=> 800
Collapse
 
pbouillon profile image
Pierre Bouillon • Edited

Here goes Python !

min(nums, key=lambda x: abs(x - given_num))