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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:52:28+00:00 2026-06-17T00:52:28+00:00

I have almost completed the code for this problem, which I shall state as

  • 0

I have almost completed the code for this problem, which I shall state as under:

Given:

Array of length ‘n’ (say n = 10000) declared as below,

     char **records = malloc(10000*sizeof(*records));

Each record[i] is a char pointer and points to a non-empty string.

     records[i] = malloc(11);

The strings are of fixed length (10 chars + ‘\0’).

Requirement:

Return the most frequently occurring string in the above array.

But now, I am interested in obtaining a slightly less brutal algorithm than the primitive one which I have currently, which is to sift through the entire array in two for loops :(, storing strings encountered by the two loops in a temporary array of similar size (‘n’ – in case all are unique strings) for comparison with the next strings. The inner loop iterates from ‘outer loop position + 1‘ to ‘n‘. At the same time, I have an integer array, of similar size – ‘n’, for counting repeat occurrences, with each i th element corresponding to the i th (unique) string in the comparison array. Then find the largest integer and use its index in the comparison array to return the most frequently occurring string.

I hope I am clear enough. I am quite ashamed of the algo myself, but it had to be done. I am sure there is a much smarter way to do this in C.

Have a great Sunday,

Cheers!

  • 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-17T00:52:29+00:00Added an answer on June 17, 2026 at 12:52 am

    In most languages, the usual approach would be to construct a hashtable, mapping strings to counts. This has O(N) complexity.

    For example, in Python (although usually you would use collections.Counter for this, and even this code can be made more concise using more specialised Python knowledge, but I’ve made it explicit for demonstration).

    def most_common(strings):
        counts = {}
        for s in strings:
            if s not in counts:
                counts[s] = 0
            counts[s] += 1
        return max(counts, key=counts.get)
    

    But in C, you don’t have a hashtable in the standard library (although in C++ you can use hash_map from the STL), so a sort and scan can be done instead. It’s O(N.log(N)) complexity, which is worse than optimal, but quite practical.

    Here’s some C (actually C99) code that implements this.

    int compare_strings(const void*s0, const void*s1) {
        return strcmp((const char*)s0, (const char*)s1);
    }
    
    const char *most_common(const char **records, size_t n) {
        qsort(records, n, sizeof(records[0]), compare_strings);
        const char *best = 0;  // The most common string found so far.
        size_t max = 0;  // The longest run found.
        size_t run = 0;  // The length of the current run.
        for (size_t i = 0; i < n; i++) {
            if (!compare_strings(records[i], records[i - run])) {
                run += 1;
            } else {
                run = 1;
            }
            if (run > max) {
                best = records[i];
                max = run;
            }
         }
         return best;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I almost have this Powershell script completed but I am stuck at the last
This is almost identical problem which I faced a few days ago . I
I have got the following problem with F# code recently: almost the complete code
I'm having some problems building an hierarchistic array structure - I have almost got
Hey all, got one mountain of a problem here. I have completed a program
Here's the problem: I have an (almost) empty aspx page and I want to
I have almost solved a huge problem with png files and transparency in IE
I have a CPP code which uses openmp. It is linked to a fortran90
I have a code in which I #include<linux/videodev2.h> . There are three files: one
I have almost completed my prototype registration page for a club that wants to

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.