Sorting characters in a string first by frequency and then alphabetically

Viewed 12966

Given a string, I'm trying to count the occurrence of each letter in the string and then sort their frequency from highest to lowest. Then, for letters that have similar number of occurrences, I have to sort them alphabetically.

Here is what I have been able to do so far:

  • I created an int array of size 26 corresponding to the 26 letters of the alphabet with individual values representing the number of times it appeared in the sentence
  • I pushed the contents of this array into a vector of pairs, v, of int and char (int for the frequency, and char for the actual letter)
  • I sorted this vector of pairs using std::sort(v.begin(), v.end());

In displaying the frequency count, I just used a for loop starting from the last index to display the result from highest to lowest. I am having problems, however, with regard to those letters having similar frequencies, because I need them displayed in alphabetical order. I tried using a nested for loop with the inner loop starting with the lowest index and using a conditional statement to check if its frequency is the same as the outer loop. This seemed to work, but my problem is that I can't seem to figure out how to control these loops so that redundant outputs will be avoided. To understand what I'm saying, please see this example output:

Enter a string: hello world

Pushing the array into a vector pair v:
d = 1
e = 1
h = 1
l = 3
o = 2
r = 1
w = 1


Sorted first according to frequency then alphabetically:
l = 3
o = 2
d = 1
e = 1
h = 1
r = 1
w = 1
d = 1
e = 1
h = 1
r = 1
d = 1
e = 1
h = 1
d = 1
e = 1
d = 1
Press any key to continue . . .

As you can see, it would have been fine if it wasn't for the redundant outputs brought about by the incorrect for loops.

If you can suggest more efficient or better implementations with regard to my concern, then I would highly appreciate it as long as they're not too complicated or too advanced as I am just a C++ beginner.

If you need to see my code, here it is:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    cout<<"Enter a string: ";
    string input;
    getline(cin, input);

    int letters[26]= {0};

    for (int x = 0; x < input.length(); x++) {
        if (isalpha(input[x])) {
            int c = tolower(input[x] - 'a');
            letters[c]++;
        }
    }

    cout<<"\nPushing the array into a vector pair v: \n";
    vector<pair<int, char> > v;

    for (int x = 0; x < 26; x++) {
        if (letters[x] > 0) {
            char c = x + 'a';
            cout << c << " = " << letters[x] << "\n";
            v.push_back(std::make_pair(letters[x], c));
        }
    }

    // Sort the vector of pairs.
    std::sort(v.begin(), v.end());

    // I need help here!
    cout<<"\n\nSorted first according to frequency then alphabetically: \n";
    for (int x = v.size() - 1 ; x >= 0; x--) {
        for (int y = 0; y < x; y++) {
            if (v[x].first == v[y].first) {
                cout << v[y].second<< " = " << v[y].first<<endl;
            }
        }
        cout << v[x].second<< " = " << v[x].first<<endl;
    }

    system("pause");
    return 0;
}
7 Answers

(Posted on behalf of the OP.)

Thanks to the responses of the awesome people here at Stack Overflow, I was finally able to fix my problem. Here is my final code in case anyone is interested or for future references of people who might be stuck in the same boat:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

struct Letters
{
    Letters() : freq(0){}
    Letters(char letter,int freq) {
        this->freq = freq;
        this->letter = letter;
    }
    char letter;
    int freq;
};

bool Greater(const Letters& a, const Letters& b)
{
    if(a.freq == b.freq)
        return a.letter < b.letter;

    return a.freq > b.freq;
}

int main () {

    cout<<"Enter a string: ";
    string input;
    getline(cin, input);

    vector<Letters> count;
    int letters[26]= {0};

    for (int x = 0; x < input.length(); x++) {
        if (isalpha(input[x])) {
            int c = tolower(input[x] - 'a');
            letters[c]++;
        }
    }

    for (int x = 0; x < 26; x++) {
        if (letters[x] > 0) {
            char c = x + 'a';
            count.push_back(Letters(c, letters[x]));
        }
    }

    cout<<"\nUnsorted list..\n";
    for (int x = 0 ; x < count.size(); x++) {
        cout<<count[x].letter<< " = "<< count[x].freq<<"\n";
    }

    std::sort(count.begin(),count.end(),Greater);

    cout<<"\nSorted list according to frequency then alphabetically..\n";
    for (int x = 0 ; x < count.size(); x++) {
        cout<<count[x].letter<< " = "<< count[x].freq<<"\n";
    }

    system("pause");
    return 0;
}

Example output:

Enter a string: hello world

Unsorted list..
d = 1
e = 1
h = 1
l = 3
o = 2
r = 1
w = 1

Sorted list according to frequency then alphabetically..
l = 3
o = 2
d = 1
e = 1
h = 1
r = 1
w = 1
Press any key to continue . . .

I basically just followed the advice of @OliCharlesworth and implemented a custom comparator through the help of this guide: A Function Pointer as Comparison Function.

Although I'm pretty sure that my code can still be made more efficient, I'm still pretty happy with the results.

// CODE BY VIJAY JANGID in C language
// Using arrays, Time complexity - ( O(N) * distinct characters ) 
// Efficient answer

#include <stdio.h>

int main() {

    int iSizeFrequencyArray= 58;
    //  122 - 65 = 57  for A to z
    int frequencyArray[iSizeFrequencyArray]; 

    int iIndex = 0;

    // Initializing frequency to zero for all
    for (iIndex = 0; iIndex < iSizeFrequencyArray; iIndex++) {
        frequencyArray[iIndex] = 0;
    }

    int iMyStringLength = 1000;
    char chMyString[iMyStringLength];

    // take input for the string
    scanf("%s", &chMyString);

    // calculating length
    int iSizeMyString;
    while(chMyString[++iSizeMyString]);

    // saving each character frequency in the freq. array
    for (iIndex = 0; iIndex < iSizeMyString; iIndex++) {
        int currentChar = chMyString[iIndex];
        frequencyArray[currentChar - 65]++;
    }

    /* // To print the frequency of each alphabet
    for (iIndex = 0; iIndex < iSizeFrequencyArray; iIndex++) {
        char currentChar = iIndex + 65;
        printf("\n%c - %d", currentChar, frequencyArray[iIndex ]);
    }
    */

    int lowestDone = 0, lowest = 0, highestSeen = 0;

    for( iIndex = 0; iIndex < iSizeFrequencyArray; iIndex++ ) {
        if(frequencyArray[iIndex] > highestSeen) {
            highestSeen = frequencyArray[iIndex];
        }
    }

    // assigning sorted values to the current array
    while (lowest != highestSeen) {

        // calculating lowest frequency
        for( iIndex = 0; iIndex < iSizeFrequencyArray; iIndex++ ) {

            if( frequencyArray[iIndex] > lowestDone &&
               frequencyArray[iIndex] < lowest) {
                lowest = frequencyArray[iIndex]; // taking lowest value
            }
        }

        // printing that frequency
        for( iIndex =0; iIndex < iSizeFrequencyArray; iIndex++ ) {

            // print that work for that times
            if(frequencyArray[iIndex] == lowest){
                char currentChar = iIndex + 65;
                int iIndex3;
                for(iIndex3 = 0; iIndex3 < lowest; iIndex3++){
                    printf("%c", currentChar);
                }
            }
        }

        // now that is done, move to next lowest
        lowestDone = lowest;

        // reset to highest value, to get the next lowest one
        lowest = highestSeen+1;

    }

    return 0;

}

Explanation:

  1. First create array to store repetition of size (112 - 65) to store asci characters from A to z.
  2. Store the frequency of each character by incrementing at each occurrence.
  3. Now find the highest frequency.
  4. Run a loop where condition is (lowest != highest) where lowest = 0 initially.
  5. Now in each iteration print character which whose frequency is equal to lowest. They will be alphabetically in order automatically.
  6. At last find the next higher frequency and print then so on.
  7. When lowest reach highest then break loop.

Using an unordered_map for counting characters as suggested by @Manu343726 is a good idea. However, in order to produce your sorted output, another step is required.

My solution is also in C++11 and uses a lambda expression. This way you neither need to define a custom struct nor a comparison function. The code is almost complete, I just skipped reading the input:

#include <unordered_map>
#include <iostream>
#include <set>

int main() {
    string input = "hello world";

    unordered_map<char, unsigned int> count;
    for (char character : input)
        if (character >= 'a' && character <= 'z')
            count[character]++;

    cout << "Unsorted list:" << endl;
    for (auto const &kv : count)
        cout << kv.first << " = " << kv.second << endl;

    using myPair = pair<char, unsigned int>;
    auto comp = [](const myPair& a, const myPair& b) {
        return (a.second > b.second || a.second == b.second && a.first < b.first);
    };
    set<myPair, decltype(comp)> sorted(comp);
    for(auto const &kv : count)
        sorted.insert(kv);

    cout << "Sorted list according to frequency then alphabetically:" << endl;
    for (auto const &kv : sorted)
        cout << kv.first << " = " << kv.second << endl;

    return 0;
}

Output:

Unsorted list:
r = 1
h = 1
e = 1
d = 1
o = 2
w = 1
l = 3
Sorted list according to frequency then alphabetically:
l = 3
o = 2
d = 1
e = 1
h = 1
r = 1
w = 1

Note 1: Instead of inserting each element from the unordered_map into the set, it might be more efficient to use the function std::transform or std:copy, but my code is at least short.

Note 2: Instead of using a custom sorted set which maintains the order you want, it might be more efficient to use a vector of pairs and sort it once in the end, but your solution is already similar to this.

Code on Ideone

#include<stdio.h>

// CODE BY AKSHAY BHADERIYA

char iFrequencySort (char iString[]);
void vSort (int arr[], int arr1[], int len);

int
main ()
{
  int iLen, iCount;
  char iString[100], str[100];
  printf ("Enter a string : ");
  scanf ("%s", iString);

  iFrequencySort (iString);

  return 0;
}


char
iFrequencySort (char iString[])
{
  int iFreq[100] = { 0 };
  int iI, iJ, iK, iAsc, iLen1 = 0, iLen = 0;

  while (iString[++iLen]);

  int iOccurrence[94];
  int iCharacter[94];

  for (iI = 0; iI < iLen; iI++)
    {               //frequency of the characters
      iAsc = (int) iString[iI];
      iFreq[iAsc - 32]++;
    }



  for (iI = 0, iJ = 0; iI < 94; iI++)
    {               //the characters and occurrence arrays
      if (iFreq[iI] != 0)
    {
      iCharacter[iJ] = iI;
      iOccurrence[iJ] = iFreq[iI];
      iJ++;
    }
    }
  iLen1 = iJ;

  vSort (iOccurrence, iCharacter, iLen1);   //sorting both arrays

  /*letter array consists only the index of iFreq array.
     Converting it to the ASCII value of corresponding character */
  for (iI = 0; iI < iLen1; iI++)
    {
      iCharacter[iI] += 32;
    }
  iK = 0;
  for (iI = 0; iI < iLen1; iI++)
    {               //characters into original string
      for (iJ = 0; iJ < iOccurrence[iI]; iJ++)
    {
      iString[iK++] = (char) iCharacter[iI];
    }
    }
  printf ("%s", iString);
}

void
vSort (int iOccurrence[], int iCharacter[], int len)
{
  int iI, iJ, iTemp;
  for (iI = 0; iI < len - 1; iI++)
    {
      for (iJ = iI + 1; iJ < len; iJ++)
    {
      if (iOccurrence[iI] > iOccurrence[iJ])
        {
          iTemp = iOccurrence[iI];
          iOccurrence[iI] = iOccurrence[iJ];
          iOccurrence[iJ] = iTemp;

          iTemp = iCharacter[iI];
          iCharacter[iI] = iCharacter[iJ];
          iCharacter[iJ] = iTemp;
        }
    }
    }
}

Answers are given and one is accepted. I would like to give an additional answer showing the standard approach for this task.

There is often the requirement to first count things and then to get back their rank or some topmost value or other information.

One of the most common solution is to use a so called associative container for that, and, here specifically, a std::map or even better a std::unordered_map. This, because we need a key value, in the above described way a letter and an associted value, here the count for this letter. The key is unique. There cannot be more than one of the same letter in it. This would of course not make any sense.

Associative containers are very efficient by accessing their elements by their key value.

OK, there are 2 of them. The std::map and the std::unordered_map. One uses a tree to store the key in a sorted manner and the other use fast hashing algorithms to access the key values. Since we are later not interested in sorted keys, but in sorted count of occurence, we can choose the std::unordred_map. As a futher benefit, this will use fast the hashing algorithms mentioned to access a key.

The maps have an additional huge advantage. The have an index operator [], that will look very fast for a key value. If found, it will return a reference to the value associated with the key. If not found, it will create a key and initialize its value with the default (0 in our case). And then counting of any key is as simple as map[key]++.

But then, later, we here often hear: But it must be sorted by the count. That does of course not work, because the count my have duplicate values, and the map can only contain unique key values. So, impossible.

The solution is to use a second associative container a std::multiset which can have more of the same keys and a custome sort operator, where we can sort according to the value. In this we store the not a key and a value as 2 elements, but a std::pair with both values. And we sort by the 2nd part of the pair.

We cannot use a std::multi:set in the first place, because we need the unique key (in this case the letter).

The above described approach gives us extreme flexibility and ease of use. We can basically count anything with this algorithm

It could for example look the the below compact code:

#include <iostream>
#include <string>
#include <utility>
#include <set>
#include <unordered_map>
#include <type_traits>
#include <cctype>

// ------------------------------------------------------------
// Create aliases. Save typing work and make code more readable
using Pair = std::pair<char, unsigned int>;

// Standard approach for counter
using Counter = std::unordered_map<Pair::first_type, Pair::second_type>;

// Sorted values will be stored in a multiset
struct Comp { bool operator ()(const Pair& p1, const Pair& p2) const { return (p1.second == p2.second) ? p1.first<p2.first : p1.second>p2.second; } };
using Rank = std::multiset<Pair, Comp>;
// ------------------------------------------------------------


// --------------------------------------------------------------------------------------
// Compact function to calculate the frequency of charcters and then get their rank
Rank getRank(std::string& text) {

    // Definition of our counter
    Counter counter{};

    // Iterate over all charcters in text and count their frequency
    for (const char c : text) if (std::isalpha(c)) counter[char(std::tolower(c))]++;
    
    // Return ranks,sorted by frequency and then sorted by character
    return { counter.begin(), counter.end() };
}
// --------------------------------------------------------------------------------------
// Test, driver code
int main() {
    // Get a string from the user
    if (std::string text{}; std::getline(std::cin, text))

        // Calculate rank and show result
        for (const auto& [letter, count] : getRank(text))
            std::cout << letter << " = " << count << '\n';
}

Please see the minimal statements used. Very elegant.


But often we do see that arrays are use as an associted container. They have also an index (a key) and a value. Disadvantage may be a tine space overhead for unsued keys. Additionally the will only work for something wit a know magnitude. For example for 26 letters. Other countries alphabets may have more or less letters. Then this kind of solution would be not that flexible. Anyway it is also often used and OK.

So, your solution maybe a littel bit more complex, but will of course still work.

Let me give you an additional example for getting the topmost value of any container. Here you will see, how flexible such a solution can be.

I am sorry, but it is a little bit advanced. . .

#include <iostream>
#include <utility>
#include <unordered_map>
#include <queue>
#include <vector>
#include <iterator>
#include <type_traits>
#include <string>


// Helper for type trait We want to identify an iterable container ----------------------------------------------------
template <typename Container>
auto isIterableHelper(int) -> decltype (
    std::begin(std::declval<Container&>()) != std::end(std::declval<Container&>()),     // begin/end and operator !=
    ++std::declval<decltype(std::begin(std::declval<Container&>()))&>(),                // operator ++
    void(*std::begin(std::declval<Container&>())),                                      // operator*
    void(),                                                                             // Handle potential operator ,
    std::true_type{});
template <typename T>
std::false_type isIterableHelper(...);

// The type trait -----------------------------------------------------------------------------------------------------
template <typename Container>
using is_iterable = decltype(isIterableHelper<Container>(0));

// Some Alias names for later easier reading --------------------------------------------------------------------------
template <typename Container>
using ValueType = std::decay_t<decltype(*std::begin(std::declval<Container&>()))>;
template <typename Container>
using Pair = std::pair<ValueType<Container>, size_t>;
template <typename Container>
using Counter = std::unordered_map<ValueType<Container>, size_t>;
template <typename Container>
using UnderlyingContainer = std::vector<Pair<Container>>;

// Predicate Functor
template <class Container> struct LessForSecondOfPair {
    bool operator () (const Pair<Container>& p1, const Pair<Container>& p2) { return p1.second < p2.second; }
};
template <typename Container>
using MaxHeap = std::priority_queue<Pair<Container>, UnderlyingContainer<Container>, LessForSecondOfPair<Container>>;


// Function to get most frequent used number in any Container ---------------------------------------------------------
template <class Container>
auto topFrequent(const Container& data) {

    if constexpr (is_iterable<Container>::value) {

        // Count all occurences of data
        Counter<Container> counter{};
        for (const auto& d : data) counter[d]++;

        // Build a Max-Heap
        MaxHeap<Container> maxHeap(counter.begin(), counter.end());

        // Return most frequent number
        return maxHeap.top().first;
    }
    else
        return data;
}
// Test
int main() {
    std::vector testVector{ 1,2,2,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,6,7 };
    std::cout << "Most frequent is: " << topFrequent(testVector) << "\n";

    double cStyleArray[] = { 1.1, 2.2, 2.2, 3.3, 3.3, 3.3 };
    std::cout << "Most frequent is: " << topFrequent(cStyleArray) << "\n";

    std::string s{ "abbcccddddeeeeeffffffggggggg" };
    std::cout << "Most frequent is: " << topFrequent(s) << "\n";

    double value = 12.34;
    std::cout << "Most frequent is: " << topFrequent(value) << "\n";

    return 0;
}
Related