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

  • Home
  • SEARCH
  • 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 8741629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:21:02+00:00 2026-06-13T11:21:02+00:00

How can I design an algorithm which can return the 10 most frequently used

  • 0

How can I design an algorithm which can return the 10 most frequently used words in a document in O(n) time? If additional space can be used.

I can parse and place the words in a hash map with count . But next I have to sort the values to get the most frequent ones . Also I have to have a mapping btw the values -> Key which cannot be maintained since values may be repeating.

So how can I solve this ?

  • 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-13T11:21:03+00:00Added an answer on June 13, 2026 at 11:21 am

    It may be done in O(n) if you use the correct data structure.

    Consider a Node, consisting of 2 things:

    • A counter (initially set to 0).
    • An array of 255 (or whatever number of characters) pointers to Node. All the pointers are initially set to NULL.

    Create a root node. Define a “current” Node pointer, set it to root node initially.
    Then walk through all the characters of the document and do the following:

    • If the next characters is not a space – pick the appropriate pointer from the array of the current node. If it’s NULL – allocate it. The current Node pointer is updated.
    • If it’s a space (or whatever word delimiter) – increment the counter of the “current” Node. Then reset the “current” Node pointer to point to the root node.

    By such you build a tree in O(n). Every element (both node and leave) denote a specific word, together with its counter.

    Then transverse the tree to find the node with the largest counter. It’s also O(n), since the number of elements in the tree is not bigger than O(n).

    Update:

    The last step is not mandatory. Actually the most common word may be updated during the character processing.
    The following is a pseudo-code:

    struct Node
    {
        size_t m_Counter;
        Node* m_ppNext[255];
        Node* m_pPrev;
    
        Node(Node* pPrev) :m_Counter(0)
        {
            m_pPrev = pPrev;
            memset(m_ppNext, 0, sizeof(m_ppNext));
        }
        ~Node()
        {
            for (int i = 0; i < _countof(m_ppNext) i++)
                if (m_ppNext[i])
                    delete m_ppNext[i];
        }
    
    };
    
    Node root(NULL);
    Node* pPos = &root;
    Node* pBest = &root;
    char c;
    
    while (0 != (c = GetNextDocumentCharacter()))
    {
        if (c == ' ')
        {
            if (pPos != &root)
            {
                pPos->m_Counter++;
    
                if (pBest->m_Counter < pPos->m_Counter)
                    pBest = pPos;
    
                pPos = &root;
            }
        } else
        {
            Node*& pNext = pPos->m_ppNext[c - 1];
            if (!pNext)
                pNext = new Node(pPos);
            pPos = pNext;
        }
    }
    
    // pBest points to the most common word. Using pBest->m_pPrev we iterate in reverse order through its characters
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was asked to design an algorithm to calculate most user viewed pages. I
I came across this variation of edit-distance problem: Design an algorithm which transforms a
Design an algorithm to find all pairs of integers within an array which sum
I'm looking to see if I can design a HtmlHelper extension method that will
How can I design my xsd to ignore the sequence of elements? <root> <a/>
I am making a custom module where users can upload design content. I need
I'm trying to figure out what's the best design I can go with for
Can we use whole page design jpg file as css-sprite without slicing?
how can i find out if my database design (specially relationships) are ok? i
I can not find how to implement a design in C++. In the language

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.