Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to find the most frequent value of string array, and how many

How to find the most frequent value of string array, and how many

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsdata-structurestutorialquestion
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sadas232341s
    wrote on last edited by
    #1

    I have a vector string array filled as follows: "s1" "v1" "s1" "f1" "g1" "s1" "s1" "o1" so the result has to be "s1 4". I have to write the string value and the num value in strArtist and iNum. How's that?

    void maxCollByArtist(string& strArtist, int& iNum)
    {
    int max = 0;
    string t_str = "";
    vector temp;

    for(int i = 0; i < m\_vMusic.size(); i++)
    	temp\[i\] = m\_vMusic\[i\].GetArtist();
    
    for(int i = 0; i < temp.size(); i++)
    {
    	//if(temp\[i\] == t\_str)	
    }
    

    }

    C 1 Reply Last reply
    0
    • S sadas232341s

      I have a vector string array filled as follows: "s1" "v1" "s1" "f1" "g1" "s1" "s1" "o1" so the result has to be "s1 4". I have to write the string value and the num value in strArtist and iNum. How's that?

      void maxCollByArtist(string& strArtist, int& iNum)
      {
      int max = 0;
      string t_str = "";
      vector temp;

      for(int i = 0; i < m\_vMusic.size(); i++)
      	temp\[i\] = m\_vMusic\[i\].GetArtist();
      
      for(int i = 0; i < temp.size(); i++)
      {
      	//if(temp\[i\] == t\_str)	
      }
      

      }

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      if you sort the vector, identical items will be next to each other so it will be simpler to count which of them repeats the most

      image processing toolkits | batch image processing

      S 1 Reply Last reply
      0
      • C Chris Losinger

        if you sort the vector, identical items will be next to each other so it will be simpler to count which of them repeats the most

        image processing toolkits | batch image processing

        S Offline
        S Offline
        sadas232341s
        wrote on last edited by
        #3

        I did it. Now how to see which element repeats the most and how many times? (Created a new project)

        int main()
        {
        string str[8] = {"zxc", "zxc", "asd", "qwe", "zxc", "asd", "qwe", "jkl"};
        vector v_str;
        string t_str;
        int iNum = 0;

        for(int i = 0; i < 8; i++)
        	v\_str.push\_back(str\[i\]);
        
        for(int i = 1; i < v\_str.size(); i++)
        	for(int j = v\_str.size() - 1; j >= i; j--)
        		if(v\_str\[j - 1\] > v\_str\[j\])
        		{
        			t\_str = v\_str\[j - 1\];
        			v\_str\[j - 1\] = v\_str\[j\];
        			v\_str\[j\] = t\_str;
        		}
        
        for(int i = 0; i < v\_str.size(); i++)
        	cout << v\_str\[i\] << endl;
        
        return 0;
        

        }

        C L 2 Replies Last reply
        0
        • S sadas232341s

          I did it. Now how to see which element repeats the most and how many times? (Created a new project)

          int main()
          {
          string str[8] = {"zxc", "zxc", "asd", "qwe", "zxc", "asd", "qwe", "jkl"};
          vector v_str;
          string t_str;
          int iNum = 0;

          for(int i = 0; i < 8; i++)
          	v\_str.push\_back(str\[i\]);
          
          for(int i = 1; i < v\_str.size(); i++)
          	for(int j = v\_str.size() - 1; j >= i; j--)
          		if(v\_str\[j - 1\] > v\_str\[j\])
          		{
          			t\_str = v\_str\[j - 1\];
          			v\_str\[j - 1\] = v\_str\[j\];
          			v\_str\[j\] = t\_str;
          		}
          
          for(int i = 0; i < v\_str.size(); i++)
          	cout << v\_str\[i\] << endl;
          
          return 0;
          

          }

          C Offline
          C Offline
          Chris Losinger
          wrote on last edited by
          #4

          or, just use:

          std::sort(v_str.begin(), v_str.end())

          so if you have a sorted list of strings: aaa, aaa, bb, cc, ccc, dd, dd, dd, dd, e, f, g iterate through the list, check to see when the current item is equal to the last item. if it's the same, keep track of how many times it repeats. if it's different, see if the number of repetitions was greater than the max number of reps you've seen so far.

          cur = vec[0]
          maxrep = 1
          maxstring = cur
          currep = 1

          for each item in vec, starting at vec[1]
          if item==cur
          currep++ // we have a repeat, keep track of how many repetitions
          else
          // the strings are different,
          if currep > maxrep // was this repetition count higher than the max
          maxrep=currep // it is the new max
          maxstring=cur
          cur=item // next item is current
          currep = 0

          image processing toolkits | batch image processing

          S 1 Reply Last reply
          0
          • C Chris Losinger

            or, just use:

            std::sort(v_str.begin(), v_str.end())

            so if you have a sorted list of strings: aaa, aaa, bb, cc, ccc, dd, dd, dd, dd, e, f, g iterate through the list, check to see when the current item is equal to the last item. if it's the same, keep track of how many times it repeats. if it's different, see if the number of repetitions was greater than the max number of reps you've seen so far.

            cur = vec[0]
            maxrep = 1
            maxstring = cur
            currep = 1

            for each item in vec, starting at vec[1]
            if item==cur
            currep++ // we have a repeat, keep track of how many repetitions
            else
            // the strings are different,
            if currep > maxrep // was this repetition count higher than the max
            maxrep=currep // it is the new max
            maxstring=cur
            cur=item // next item is current
            currep = 0

            image processing toolkits | batch image processing

            S Offline
            S Offline
            sadas232341s
            wrote on last edited by
            #5

            It's not working. Gives me "zxc" and 1, but the correct is "qwe" and 3.

            int main()
            {
            string str[8] = {"zxc", "zxc", "asd", "qwe", "qwe", "asd", "qwe", "jkl"};
            vector v_str;
            string t_str, maxstring;
            int maxrep = 1;
            int currep = 1;

            for(int i = 0; i < 8; i++)
            	v\_str.push\_back(str\[i\]);
            
            for(int i = 1; i < v\_str.size(); i++)
            	for(int j = v\_str.size() - 1; j >= i; j--)
            		if(v\_str\[j - 1\] > v\_str\[j\])
            		{
            			t\_str = v\_str\[j - 1\];
            			v\_str\[j - 1\] = v\_str\[j\];
            			v\_str\[j\] = t\_str;
            		}
            
            t\_str = v\_str\[0\];
            maxstring = t\_str;
            
            for(int i = 1; i < v\_str.size(); i++)
            {
            	if(v\_str\[i\] == t\_str)
            		currep++;
            	else if(currep > maxrep)
            	{
            		maxrep = currep;
            		maxstring = t\_str;
            	}
            
            	t\_str = v\_str\[i\];
            	currep = 0;
            }
            
            for(int i = 0; i < v\_str.size(); i++)
            	cout << v\_str\[i\] << endl;
            
            cout << endl;
            
            cout << t\_str << endl;
            cout << maxrep << endl;
            
            return 0;
            

            }

            C 1 Reply Last reply
            0
            • S sadas232341s

              It's not working. Gives me "zxc" and 1, but the correct is "qwe" and 3.

              int main()
              {
              string str[8] = {"zxc", "zxc", "asd", "qwe", "qwe", "asd", "qwe", "jkl"};
              vector v_str;
              string t_str, maxstring;
              int maxrep = 1;
              int currep = 1;

              for(int i = 0; i < 8; i++)
              	v\_str.push\_back(str\[i\]);
              
              for(int i = 1; i < v\_str.size(); i++)
              	for(int j = v\_str.size() - 1; j >= i; j--)
              		if(v\_str\[j - 1\] > v\_str\[j\])
              		{
              			t\_str = v\_str\[j - 1\];
              			v\_str\[j - 1\] = v\_str\[j\];
              			v\_str\[j\] = t\_str;
              		}
              
              t\_str = v\_str\[0\];
              maxstring = t\_str;
              
              for(int i = 1; i < v\_str.size(); i++)
              {
              	if(v\_str\[i\] == t\_str)
              		currep++;
              	else if(currep > maxrep)
              	{
              		maxrep = currep;
              		maxstring = t\_str;
              	}
              
              	t\_str = v\_str\[i\];
              	currep = 0;
              }
              
              for(int i = 0; i < v\_str.size(); i++)
              	cout << v\_str\[i\] << endl;
              
              cout << endl;
              
              cout << t\_str << endl;
              cout << maxrep << endl;
              
              return 0;
              

              }

              C Offline
              C Offline
              Chris Losinger
              wrote on last edited by
              #6

              else if(currep > maxrep)
              {
              maxrep = currep;
              maxstring = t_str;
              }

              	t\_str = v\_str\[i\];
              	currep = 0;
              

              should be:

              	else 
              	{
              		if(currep > maxrep)
              		{
              			maxrep = currep;
              			maxstring = t\_str;
              		}
              
              		t\_str = v\_str\[i\];
              		currep = 0;
              	}
              

              also, you will have to add a check after the loop, to see if the last repetition has not ended.

              image processing toolkits | batch image processing

              S 2 Replies Last reply
              0
              • C Chris Losinger

                else if(currep > maxrep)
                {
                maxrep = currep;
                maxstring = t_str;
                }

                	t\_str = v\_str\[i\];
                	currep = 0;
                

                should be:

                	else 
                	{
                		if(currep > maxrep)
                		{
                			maxrep = currep;
                			maxstring = t\_str;
                		}
                
                		t\_str = v\_str\[i\];
                		currep = 0;
                	}
                

                also, you will have to add a check after the loop, to see if the last repetition has not ended.

                image processing toolkits | batch image processing

                S Offline
                S Offline
                sadas232341s
                wrote on last edited by
                #7

                " to see if the last repetition has not ended." what does that mean?

                1 Reply Last reply
                0
                • S sadas232341s

                  I did it. Now how to see which element repeats the most and how many times? (Created a new project)

                  int main()
                  {
                  string str[8] = {"zxc", "zxc", "asd", "qwe", "zxc", "asd", "qwe", "jkl"};
                  vector v_str;
                  string t_str;
                  int iNum = 0;

                  for(int i = 0; i < 8; i++)
                  	v\_str.push\_back(str\[i\]);
                  
                  for(int i = 1; i < v\_str.size(); i++)
                  	for(int j = v\_str.size() - 1; j >= i; j--)
                  		if(v\_str\[j - 1\] > v\_str\[j\])
                  		{
                  			t\_str = v\_str\[j - 1\];
                  			v\_str\[j - 1\] = v\_str\[j\];
                  			v\_str\[j\] = t\_str;
                  		}
                  
                  for(int i = 0; i < v\_str.size(); i++)
                  	cout << v\_str\[i\] << endl;
                  
                  return 0;
                  

                  }

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  TCPMem wrote:

                  I did it. Now how to see which element repeats the most and how many times?

                  TCPMem, Rather than re-inventing those algorithms... just use the ones supplied by the c++ language. string str[8] = {"zxc", "zxc", "asd", "qwe", "zxc", "asd", "qwe", "jkl"}; vector v_strAll; vector v_strUnique; list> counts; v_strAll.assign(str,str+_countof(str)); sort(v_strAll.begin(),v_strAll.end()); unique_copy(v_strAll.begin(),v_strAll.end(),back_inserter(v_strUnique)); std::vector::iterator it = v_strUnique.begin(); while(it != v_strUnique.end()) { counts.push_back(make_pair(*it,count_if(v_strAll.begin(),v_strAll.end(),[it](const std::string& s1){return s1 == *it;}))); ++it; } Heh, if you really want to annoy the guy reviewing your code... you can get rid of that while loop and use a dual lambda if your compiler supports it.

                  for_each(v_strUnique.begin(),v_strUnique.end(),[&counts,v_strAll](const std::string& str){counts.push_back(make_pair(str,count_if(v_strAll.begin(),v_strAll.end(),[str](const std::string& s1){return s1 == str;})));});

                  P 1 Reply Last reply
                  0
                  • L Lost User

                    TCPMem wrote:

                    I did it. Now how to see which element repeats the most and how many times?

                    TCPMem, Rather than re-inventing those algorithms... just use the ones supplied by the c++ language. string str[8] = {"zxc", "zxc", "asd", "qwe", "zxc", "asd", "qwe", "jkl"}; vector v_strAll; vector v_strUnique; list> counts; v_strAll.assign(str,str+_countof(str)); sort(v_strAll.begin(),v_strAll.end()); unique_copy(v_strAll.begin(),v_strAll.end(),back_inserter(v_strUnique)); std::vector::iterator it = v_strUnique.begin(); while(it != v_strUnique.end()) { counts.push_back(make_pair(*it,count_if(v_strAll.begin(),v_strAll.end(),[it](const std::string& s1){return s1 == *it;}))); ++it; } Heh, if you really want to annoy the guy reviewing your code... you can get rid of that while loop and use a dual lambda if your compiler supports it.

                    for_each(v_strUnique.begin(),v_strUnique.end(),[&counts,v_strAll](const std::string& str){counts.push_back(make_pair(str,count_if(v_strAll.begin(),v_strAll.end(),[str](const std::string& s1){return s1 == str;})));});

                    P Offline
                    P Offline
                    Peter_in_2780
                    wrote on last edited by
                    #9

                    :thumbsup: +5 for the last bit. And here I was thinking that APL and Perl were the only write-only languages (not counting obscurities like Brainf*ck). Cheers, Peter

                    Software rusts. Simon Stephenson, ca 1994.

                    1 Reply Last reply
                    0
                    • C Chris Losinger

                      else if(currep > maxrep)
                      {
                      maxrep = currep;
                      maxstring = t_str;
                      }

                      	t\_str = v\_str\[i\];
                      	currep = 0;
                      

                      should be:

                      	else 
                      	{
                      		if(currep > maxrep)
                      		{
                      			maxrep = currep;
                      			maxstring = t\_str;
                      		}
                      
                      		t\_str = v\_str\[i\];
                      		currep = 0;
                      	}
                      

                      also, you will have to add a check after the loop, to see if the last repetition has not ended.

                      image processing toolkits | batch image processing

                      S Offline
                      S Offline
                      sadas232341s
                      wrote on last edited by
                      #10

                      Here is my code, but it shows "asd 2" instead of "qwe 3". Why is that?

                      #include
                      #include
                      #include
                      #include

                      using namespace std;

                      int main()
                      {
                      string str[8] = {"zxc", "zxc", "asd", "qwe", "qwe", "asd", "qwe", "jkl"};
                      vector v_str;
                      string t_str, maxstring;
                      int maxrep = 1;
                      int currep = 1;

                      for(int i = 0; i < 8; i++)
                      	v\_str.push\_back(str\[i\]);
                      
                      sort(v\_str.begin(), v\_str.end());
                      
                      t\_str = v\_str\[0\];
                      maxstring = t\_str;
                      
                      for(int i = 1; i < v\_str.size(); i++)
                      {
                      	if(t\_str == v\_str\[i\])
                      		currep++;
                      	else
                      	{
                      		if(currep > maxrep)
                      		{
                      			maxrep = currep;
                      			maxstring = t\_str;
                      		}
                      
                      		t\_str = v\_str\[i\];
                      		currep = 0;
                      	}
                      }
                      
                      for(int i = 0; i < v\_str.size(); i++)
                      	cout << v\_str\[i\] << endl;
                      
                      cout << endl;
                      cout << maxstring << " " << maxrep << endl;
                      
                      return 0;
                      

                      }

                      C 1 Reply Last reply
                      0
                      • S sadas232341s

                        Here is my code, but it shows "asd 2" instead of "qwe 3". Why is that?

                        #include
                        #include
                        #include
                        #include

                        using namespace std;

                        int main()
                        {
                        string str[8] = {"zxc", "zxc", "asd", "qwe", "qwe", "asd", "qwe", "jkl"};
                        vector v_str;
                        string t_str, maxstring;
                        int maxrep = 1;
                        int currep = 1;

                        for(int i = 0; i < 8; i++)
                        	v\_str.push\_back(str\[i\]);
                        
                        sort(v\_str.begin(), v\_str.end());
                        
                        t\_str = v\_str\[0\];
                        maxstring = t\_str;
                        
                        for(int i = 1; i < v\_str.size(); i++)
                        {
                        	if(t\_str == v\_str\[i\])
                        		currep++;
                        	else
                        	{
                        		if(currep > maxrep)
                        		{
                        			maxrep = currep;
                        			maxstring = t\_str;
                        		}
                        
                        		t\_str = v\_str\[i\];
                        		currep = 0;
                        	}
                        }
                        
                        for(int i = 0; i < v\_str.size(); i++)
                        	cout << v\_str\[i\] << endl;
                        
                        cout << endl;
                        cout << maxstring << " " << maxrep << endl;
                        
                        return 0;
                        

                        }

                        C Offline
                        C Offline
                        Chris Losinger
                        wrote on last edited by
                        #11

                        TCPMem wrote:

                        currep = 0;

                        i suspect it's because you're setting currep = 1, the first time, but 0 all the other times. the first set of matches will have a higher rep count than the others.

                        image processing toolkits | batch image processing

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups