Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7797569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:43:35+00:00 2026-06-01T23:43:35+00:00

int[] array = new int[5]{5,7,8,15,20}; int TargetNumber = 13; For a target number, I

  • 0
int[] array = new int[5]{5,7,8,15,20};

int TargetNumber = 13;

For a target number, I want to find the closest number in an array. For example, when the target number is 13, the closest number to it in the array above is 15. How would I accomplish that programmatically in C#?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:43:36+00:00Added an answer on June 1, 2026 at 11:43 pm

    EDIT: Have adjusted the queries below to convert to using long arithmetic, so that we avoid overflow issues.

    I would probably use MoreLINQ‘s MinBy method:

    var nearest = array.MinBy(x => Math.Abs((long) x - targetNumber));
    

    Or you could just use:

    var nearest = array.OrderBy(x => Math.Abs((long) x - targetNumber)).First();
    

    … but that will sort the whole collection, which you really don’t need. It won’t make much difference for a small array, admittedly… but it just doesn’t feel quite right, compared with describing what you’re actually trying to do: find the element with the minimum value according to some function.

    Note that both of these will fail if the array is empty, so you should check for that first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have an int-array and I want to modify it. I know that
There is an array int[] array = new int[]{6,4,10,7,7,9}; and a number 8. I
i have an array like below int[] array = new array[n];// n may be
I have an array: int[][] lawn = new int[980][1280]; wich stores the values of
I have a prototype: int[] medianFileter(int[] data); and an array int[] intVal = new
When I use new[] to create an array of my classes: int count =
I need to declare 3d array variable but can't. int[][][] ary = new int[5][2][];
Lets tell i've got 2D array of dictionaries: public Dictionary<int, string>[,] mat = new
I have two arrays that I create like this: public int GameBoard[][] = new
I am declaring an array using new int *a = NULL; a = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.