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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:00:51+00:00 2026-06-01T04:00:51+00:00

I have a data structure of key value pairs and I want to implement

  • 0

I have a data structure of key value pairs and I want to implement “GROUP BY” value.
Both keys and values are strings.

So what I did was I gave every value(string) a unique “prime number”. Then for every key I stored the multiplication of all the prime numbers associated with different values that a particular key has.
So if key “Anirudh” has values “x”,”y”,”z”, then I store the number M(Key) = 2*3*5 = 30 as well.
Later if I want to do group by a particular value “x”(say) then I just iterate over all the keys, and divide the M(key) by the prime number associated with “x”. I then check if the remainder is 0 and if it is zero, then that particular “key” is a part of group by for value “x”.

I know that this is the most weird way to do it. Some people sort the key value pairs(sorted by values). I could have also created another table(hash table) already grouped by “values”. So I want to know a better method than mine (there must be many). In my method as the number of unique values for a particular key increases the product of prime number also increases (that too exponentially).

  • 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-01T04:00:52+00:00Added an answer on June 1, 2026 at 4:00 am

    Your method will always perform O(n) to find group members because you have to iterate through all elements of the collection to find elements belonging to the target group. Your method also risks overflowing common integer bounds (32, 64 bit) if you have many elements since you are multiplying potentially a large number of prime numbers together to form your key.

    You will find it more efficient and certainly more predictable to use a bit mask to track group membership following this approach. If you have 16 groups, you can represent that with a 16-bit short using a bit mask. Using primes as you suggest, you would need an integer with enough bits to hold the number 32589158477190044730 (first 16 primes multiplied together), which would require 65 bits.

    Other approaches to grouping also are O(n) for the first iteration (after all, each element must be tested at least once for group membership). However, if you tend to repeat the same group checks, the other methods you refer to (e.g. keeping a list or hash table per target group) is much more efficient because subsequent group membership tests are O(1).

    So to directly answer your question:

    • If there are multiple queries for group membership (repeating some groups), any solution that stores the groups (including the ones you suggest in your question) will perform better than your method.
    • If there are no repeat queries for group membership, there is no advantage to storing group membership

    Given that repeat queries seem likely based on your question:

    • Use a structure such as a list keyed off of a group ID to store group membership if you want to trade memory to get more speed.
    • Use a suitably wide bit array to store group membership if you want to trade speed to use less memory.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a system where I'll have key-value structure pairs. They will
I have a dictionary, with 300 key value pairs, where each of the keys
I have an XML Data Source which contains a list of key/value pairs. I'm
NSDictionary is good for key-value pairs, by what data structure is best for when
Suppose I have a key,value list containing 6 keys and 6 values. I wish
I have a dictionary structure, with multiple key value pairs inside. myDict.Add(key1, value1); myDict.Add(key2,
I have store 111 million key-value pairs (one key can have multiple values -
I need a memory-efficient data structure for for storing about a million key--value pairs,
I want to define an interface IFile that includes an array of key/value pairs
I have unsorted map of key value pairs. input = { xa => xavalue,

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.