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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:05:37+00:00 2026-06-18T01:05:37+00:00

Why does List<T> increase its capacity by a factor of 2? private void EnsureCapacity(int

  • 0

Why does List<T> increase its capacity by a factor of 2?

private void EnsureCapacity(int min)
{
    if (this._items.Length < min)
    {
        int num = (this._items.Length == 0) ? 4 : (this._items.Length * 2);
        if (num < min)
        {
            num = min;
        }
        this.Capacity = num;
    }
}

Why does Dictionary<K,V> use prime numbers as capacity?

private void Resize()
{
    int prime = HashHelpers.GetPrime(this.count * 2);
    int[] numArray = new int[prime];
    for (int i = 0; i < numArray.Length; i++)
    {
        numArray[i] = -1;
    }
    Entry<TKey, TValue>[] destinationArray = new Entry<TKey, TValue>[prime];
    Array.Copy(this.entries, 0, destinationArray, 0, this.count);
    for (int j = 0; j < this.count; j++)
    {
        int index = destinationArray[j].hashCode % prime;
        destinationArray[j].next = numArray[index];
        numArray[index] = j;
    }
    this.buckets = numArray;
    this.entries = destinationArray;
}

Why doesn’t it also just multiply by 2? Both are dealing with finding continues memory location…correct?

  • 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-18T01:05:38+00:00Added an answer on June 18, 2026 at 1:05 am

    It’s common to use prime numbers for hash table sizes because it reduces the probability of collisions.

    Hash tables typically use the modulo operation to find the bucket where an entry belongs, as you can see in your code:

    int index = destinationArray[j].hashCode % prime;
    

    Suppose your hashCode function results in the following hashCodes among others {x , 2x, 3x, 4x, 5x, 6x…}, then all these are going to be clustered in just m number of buckets, where m = table_length/GreatestCommonFactor(table_length, x). (It is trivial to verify/derive this). Now you can do one of the following to avoid clustering:

    1. Make sure that you don’t generate too many hashCodes that are multiples of another hashCode like in {x, 2x, 3x, 4x, 5x, 6x…}.But this may be kind of difficult if your hashTable is supposed to have millions of entries.

    2. Or simply make m equal to the table_length by making GreatestCommonFactor(table_length, x) equal to 1, i.e by making table_length coprime with x. And if x can be just about any number then make sure that table_length is a prime number.

    (from http://srinvis.blogspot.com/2006/07/hash-table-lengths-and-prime-numbers.html)

    HashHelpers.GetPrime(this.count * 2) 
    

    should return a prime number. Look at the definition of HashHelpers.GetPrime().

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

Sidebar

Related Questions

Does File.list() have a longer run time if there are more files in its
Does len(list) calculate the length of the list every time it is called, or
Why does this work: List<?> list = new LinkedList<Integer>(); while this gives a type
This query does what I need, return a list of data from a widget
does List Sort modify the collection? I think it must as I get a
Does a List have a property or mechanism by which I can prevent duplicate
I keep getting a 'List does not exist' error whenever I try and use
Possible Duplicate: C++ Vector Pointers to Objects Does std::list::remove method call destructor of each
I'm trying to build expandableList with SimpleCursorTreeAdapter but my list does not make groups,
Does some basic list operation in Haskell(e.g. ghc) are optimized equivalently to their imperative

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.