4

Say I have the following codes:

SortedDictionary<int,string> test = new SortedDictionary<int, string> ( );
test.Add ( 1, "one" );
test.Add ( 3, "three" );
test.Add ( 7, "seven" );
test.Add ( 8, "eight" );
int key = GetFirstKeyGreaterThan ( test, 3 ); // expects to get 7
int key2 = GetFirstKeyGreaterThan ( test, 6 ); // expects to get 7

Is there an easy way to implement GetFirstKeyGreaterThan method? I know we can use GetEnumerator method and call MoveNext after we reach the key of 3, but it would be an O(n) operation.

I do not want to use SortedList because I need an O(log n) for key insertion and removal.

Setyo N
  • 1,953
  • 2
  • 26
  • 28
  • You probably could try sorted collection instead of dictionary http://stackoverflow.com/questions/13131167/is-there-an-interface-like-icollectiont-but-designed-for-sorted-collections – Felix Oct 29 '12 at 23:44
  • 1
    The title of your question refers to `SortedDictionary`, while the code contains `Dictionary`. There may not be a solution to do what you'd like using either of those two, but it would help us answer if you'd clarify which one you mean. – MvanGeest Oct 29 '12 at 23:45
  • Better see this: [how-do-i-get-previous-previous-key-from-sorteddictionary](http://stackoverflow.com/questions/4720674/how-do-i-get-previous-previous-key-from-sorteddictionary). The solution is to either use a `SortedList` or `TreeDictionary` from C5. – nawfal Jun 11 '14 at 10:15

0 Answers0