Finding how many same strings are in an array:)

Hey, so i came across this little problem today. I cant figure the best way to find how many same strings are in an array. I tried checking every second string if they are the same but thats not the way to go.

Note: I arranged them all in alphabetical order to make it easier to work with, but still i cant figure it out. Any IDEAS? :D

-Thanks!!:)

example:
string array[8] = {"AAA", "AAA", "AAA", "BBB", "BBB", "CCC", "DDD", "EEE"};

So there are 3 AAA's, 2 BBB's and 3 others. So all put together tehere are 5 diffrent strings.
Last edited on
closed account (48T7M4Gy)
Since you started with them sorted it makes it easy to travel the list and at each change of value you can get the current count from the last change , reset count at change point and keep travelling, comparing last with current, and counting.
there is data structures that takes duplicates , you can then called a method to get that count. do some search with the stl.
Hi,


With the question, you had "how many same strings" at the beginning, but had " tehere are 5 diffrent strings." at the end.

So that is contradictory.

Seen as it is sorted, consider these:

This counts the A's and B's:

Starting at position 1 (the second item) if it is equal to the previous, increment a counter. Continue to the next item.

This counts the A's , B's C's D's E's:

Starting at position 1 (the second item) if it is not equal to the previous, increment a counter. Continue to the next item.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <iomanip>

int main()
{
    std::string names[] = { "AAA", "AAA", "AAA", "BBB", "BBB", "AAA", "CCC", "DDD", "BBB", "EEE", "CCC" };
    const std::size_t N = sizeof(names) / sizeof(*names) ;

    // sort the names
    // http://en.cppreference.com/w/cpp/algorithm/sort
    std::sort( names, names+N ) ;

    std::size_t n_unique_strings = 1 ; // the first string is unique

    for( std::size_t i = 1 ; i < N ; ++i ) // start with the second string
    {
        if( names[i] != names[i-1] ) // if this string is not equal to the previous string
            ++n_unique_strings ; // we have found a unique string
    }
    std::cout << "1. #unique strings: " << n_unique_strings << '\n' ;


    // remove duplicate strings (move unique strings to the front of the sorted names)
    // http://en.cppreference.com/w/cpp/algorithm/unique
    // note: the duplicated strings are move to the back and trashed (they may contain unspecified values)
    const auto end_of_unique_strings = std::unique( names, names+N ) ;

    std::cout << "2. #unique strings: " << end_of_unique_strings - names << " the unique strings are: " ;
    for( auto iter = names ; iter != end_of_unique_strings ; ++iter )
        std::cout << std::quoted(*iter) << ' ' ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/602b71243dc5f908
@JLBorges
Wow, thanks. I'll check this, for sure! :))
Topic archived. No new replies allowed.