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 3761222
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:35:33+00:00 2026-05-19T10:35:33+00:00

I have dictionary containing key value pairs. SortedDictionary<int,int> dictionary=new SortedDictionary<int,int>(); dictionary.Add(1,33); dictionary.Add(2,20); dictionary.Add(4,35); I

  • 0

I have dictionary containing key value pairs.

SortedDictionary<int,int> dictionary=new SortedDictionary<int,int>();
dictionary.Add(1,33);
dictionary.Add(2,20);
dictionary.Add(4,35);

I want to get previous key value pair from a known key value. In the above case, if I have key 4, then how can I get <2,20>?

  • 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-19T10:35:34+00:00Added an answer on May 19, 2026 at 10:35 am

    It’s hard to implement this efficiently with a SortedDictionary<TKey, TValue> since it is implemented as a binary search tree that does not expose predecessors or successors.

    You could of course just enumerate each KeyValuePair until you find the “known” key. With a little bit of LINQ, this would look like (assuming the key definitely exists and isn’t the first key):

    SortedDictionary<int, int> dictionary = ...
    int knownKey = ...
    
    var previousKvp = dictionary.TakeWhile(kvp => kvp.Key != knownKey)
                                .Last();
    

    If those assumptions don’t hold, you could do:

    var maybePreviousKvp = dictionary.TakeWhile(kvp => kvp.Key != knownKey)
                                     .Cast<KeyValuePair<int, int>?>()
                                     .LastOrDefault();
    

    (Check that maybePreviousKvp != null to ascertain that the previous KeyValuePair was retrieved successfully.)

    But this isn’t going to be efficient at all.


    If feasible, consider using a SortedList<TKey, TValue> instead (obviously, this may not be possible if you can’t take its slower inserts and deletes). This collection supports efficient key and value-retrieval by ordered index since it is implemented as a growable array. Then your query becomes as simple as:

    SortedList<int, int> dictionary = ...
    int knownKey = ...
    
    int indexOfPrevious = dictionary.IndexOfKey(knownKey) - 1;
    
    // if "known" key exists and isn't the first key
    if(indexOfPrevious >= 0)
    {
       // Wrap these in a KeyValuePair if necessary
       int previousKey = dictionary.Keys[indexOfPrevious];
       int previousValue = dictionary.Values[indexOfPrevious];      
    }
    

    IndexOfKey runs a binary search on the keys-list, running in O(log n) time. Everything else should run in constant time, meaning the entire operation should run in logarithmic time.


    Otherwise, you’ll have to implement yourself / find a BST collection that does expose predecessors / successors.

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

Sidebar

Related Questions

I have a dictionary with each key containing a list as a value. And
I have dictionary, like Dictionary<string, bool> accValues = new Dictionary<string, bool>() And I want
Does VBA have dictionary structure? Like key<>value array?
How can I get my object index? I have a dictionary containing arrays and
I'm trying to map a Dictionary containing Lists. I have the following set of
I have a Dictionary called AvailableBoxes. I want to lookup a string in this
I have a dictionary of strings that i want the user to be able
I have a System.Collections.Generic.Dictionary<string, string> containing control ID and appropriate data column to data
I have a dictionary containing the character positions of different fields in a string.
I have a dictionary of struct, where one member is a list containing varying

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.