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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:21:41+00:00 2026-05-29T06:21:41+00:00

I have a Dictionary<…, Statistics> , where Statistics is a custom structure with some

  • 0

I have a Dictionary<..., Statistics>, where Statistics is a custom structure with some fields. This struct implements the IComparer interface.

public struct Statistics : IComparer<Statistics>
{
    public UInt64 NumberOfOccurrences;
    public UInt64 TotalNumberOfWords;
    ...

    public int Compare(Statistics x, Statistics y)
    {
        // code for compare the statistics
        ...
    }
}

I chose the dictionary to have better performance during updates of existing statistics.
However, I also want good performance in reading statistics in order from best to worst, so I should use a SortedDictionary in place of Dictionary.

Now I would like to limit the number of entries within the dictionary to a specified value, by removing the entries with worst statistics, in order to limit the memory usage.
I have seen that SortedDictionary does not have constructors with a capacity argument, so I thought I’d store the maximum number of entries in the integer variable elementsCountLimit. Before adding a new entry in the SortedDictionary, I could perform the following check:

if (dict.Count == elementsCountLimit)
{
    // Remove the last entry, that is the
    // entry with worst statistics.
}
  1. But, If I don’t know the key, how can I remove the last entry?
  2. How can I limit the memory usage?
  • 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-29T06:21:42+00:00Added an answer on May 29, 2026 at 6:21 am

    Write a simple array implementation for Heap<T>. Then, use a Heap<TKey>, and a Dictionary<TKey, Statistics> together. Heap will serve as a priority queue. Careful: The top element of the heap should be the item to be deleted next. i.e. If you want to keep the “maximum” items, the heap should keep the minimum item at the root. When you will insert an item and the heap is full, also delete the root in Log(n) time. Whenever you delete an item from the heap delete it from the dictionary as well. Pseudocode:

    Insert (TKey item, Heap<TKey> heap, Dictionary<TKey, TStatistics> dictionary)
        insert (item, heap)
        if heap capacity is exceeded:
            var deleteditem = deletemin(heap)
            if(deleteditem!=item):
                insert(item, dictionary)
                delete(deleteditem,dictionary)
        else:
            insert(item,dictionary)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does VBA have dictionary structure? Like key<>value array?
I have a dictionary with following structure : {1: {'message': u'test', 'user': u'user1'}, 2:
I have a dictionary of values read from two fields in a database: a
Suppose I have Dictionary like this: Dictionary<string, string> values = new Dictionary<string, string>() {
I have a Dictionary of Dictionarys of Array of Objects. Just like this: A
I have Dictionary<long, Location> PostalCodes . While i m adding new elements to this
I have an 'dictionary' array such as this: $arr['a']=5; $arr['b']=9; $arr['as']=56; $arr['gbsdfg']=89; And I
I have a Dictionary and want to display the number of items in this
I have this dictionary in python d={1: 'a', 2: 'b', 3: 'c'} with d[1]
I have a dictionary in Python, that describes some kinds of binaries running on

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.