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

  • Home
  • SEARCH
  • 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 931383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:29:29+00:00 2026-05-15T20:29:29+00:00

I have a List<KeyValuePair<double, double>> , the list is sorted by KeyValuePair.Key , so

  • 0

I have a List<KeyValuePair<double, double>>, the list is sorted by KeyValuePair.Key, so it’s amendable to binary search. And I have a double object. Now, my task is to find the index of the double object. Here are the conditions that apply:

  1. If that double object matches one of the KeyValuePair.Key within a specified tolerance, then the corresponding KeyValuePair.Value should be returned.
  2. If the double object falls outside the max and min range of KeyValuePair.Key, then a 0 should be returned.
  3. If the double object falls within the max min of the KeyValuePair.Key, but not matched any of the KeyValuePair.Key within a specified tolerance, then the get the average of the nearest upper and nearest lower KeyValuePair.Value ( as measured by KeyValuePair.Key).

I know that a binary search implementation is available in C#, but it is not exactly suited to my needs. I would like to ask is there any implementation out there that already meets my needs? I don’t want to spend a few hours writing and debugging the code that other people has already written, debugged and perfected.

  • 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-05-15T20:29:29+00:00Added an answer on May 15, 2026 at 8:29 pm

    This can be done fairly easily with a comparer and a small wrapper around List<T>.BinarySearch:

    static double Search(List<KeyValuePair<double, double>> list, double key) {
        int index = list.BinarySearch(
            new KeyValuePair<double, double>(key, 0), 
            new Comparer());
    
         // Case 1
         if (index >= 0)
             return list[index].Value;
    
         // NOTE: if the search fails, List<T>.BinarySearch returns the 
         // bitwise complement of the insertion index that would be used
         // to keep the list sorted.
         index = ~index;
    
         // Case 2
         if (index == 0 || index == list.Count)
            return 0;
    
         // Case 3
         return (list[index - 1].Value + list[index].Value) / 2;
     }
    
     class Comparer : IComparer<KeyValuePair<double, double>> {
         public int Compare(
             KeyValuePair<double, double> x, 
             KeyValuePair<double, double> y) 
         {
             if (Math.abs(x.Key - y.Key) < TOLERANCE)
                 return 0;
    
             return x.Key.CompareTo(y.Key);
         }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 499k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The error says it all - you're trying to inject… May 16, 2026 at 12:31 pm
  • Editorial Team
    Editorial Team added an answer Not every combination of binutils, gcc, and newlib work. I… May 16, 2026 at 12:31 pm
  • Editorial Team
    Editorial Team added an answer No, not in this method. Methods that start with copy… May 16, 2026 at 12:31 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a list of keyvaluepair(string,string) first string is something like class.property, second string
I have a list of key/value pairs I'd like to store in and retrieve
I have a list of List<KeyvaluePair<DateTime, int>> , which I want to merge into
I currently have a class that uses the KeyValuePair with List to store a
Ok I have the following Code #region getDurationListDD private List<KeyValuePair<int, int>> getDurationListDD { get
I have List List<MyType> , my type contains Age and RandomID Now I want
I have a list of pairs (CarType(Enum), code(string)). Unfortunately in this collection codes are
Given the following vb.net class: Friend Class PairCollection(Of TKey, TValue) Inherits List(Of KeyValuePair(Of TKey,
I have list view to display data,it dispalys well with white color background initially,but
I have list of images and I want a Download link along with every

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.