3

Goal: Return all elements in vector A that appear N times and put results in vector B.

Expected Result:

--Begin With---

Vector A=(10,20,30,30,40,50,100,50,20,100,10,10,200,300)

Do some code to return the name of elements that appear in Vector A
when N=3

Result should be Vector B=(10) //because only 10 is in vector A N=3 times.

My Attempt: I got the counts of all the elements placed into another vector but I don't have the part that can give back all of the elements that appear N times. I'm very flexible with how it can be done if it means a speed increase.

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <iterator>     // std::back_inserter
#include <algorithm>    // std::copy

int main()
{
    std::vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };

    std::vector<std::pair<int, int> > shows;
    int target1;
    int num_items1;
    int size = static_cast<int>(v.size());

    for(int x=0; x<size; x++)
    {
        target1 = v[x];

        num_items1 = std::count(v.begin(), v.end(), target1);

        shows.push_back(std::make_pair(target1, num_items1));

        std::cout << "number: " << target1 << " count: " << num_items1 << '\n';
    } 
}

ACCEPTED SOLUTION TO QUESTION

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <iterator>     // std::back_inserter
#include <algorithm>    // std::copy
#include <set>
#include <map>

using namespace std;

int main()
{
    std::vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };

    std::vector<int> shows;
    std::map<int, int> freqOfv;

    for(const auto& i: v)
    {
        freqOfv[i]++;
    }

    std::set<int> s(v.begin(), v.end());

    int N = 2; //Can be read from stdin as well...

    for ( auto it = s.begin(); it != s.end(); it++ )

    {
        if(freqOfv[*it] ==N)
        {

            shows.push_back(*it);
        }
    }

    for (std::vector<int>::const_iterator i = shows.begin(); i != shows.end(); ++i)
    {
        std::cout << *i << ' ';
    }
    return 0;
    }
user3152377
  • 429
  • 1
  • 5
  • 17
  • 6
    You may find `std::map freq;` useful here for keeping track of the count. Every time you find an element, `freq[v[x]]++;` – user4581301 Aug 27 '18 at 02:00
  • 2
    Voted to close as too broad. – Cheers and hth. - Alf Aug 27 '18 at 02:20
  • 1
    stack overflow isn't a place to have people write the code for you. Take a shot at writing the code to store/return the types and if you have problems, post the code with the problems and say what the problems are. – xaxxon Aug 27 '18 at 02:44

1 Answers1

2

As suggested in the comments, std::map will simplify the code:

int main()
{
    std::vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };    
    std::map<int, int> freqOfv; 
    for(const auto& i: v)
        freqOfv[i]++;

    int N = 2; //Can be read from stdin as well...

    for(const auto& i: freqOfv)
    {
        if(N == i.second)
            std::cout << "The value " << i.first << " occurs " << N << " times." << std::endl;
    }   
}

This produces the following output:

The value 3 occurs 2 times.
The value 4 occurs 2 times.

Of course, you need #include <map> at the beginning to use maps in your code.

P.W
  • 26,289
  • 6
  • 39
  • 76
  • trying the code now...is there anyway to dump all of the values into an array/vector/map or whatever all at one time without having to loop into something? I have to complete this for 1000's of loops and the storing of the return values by looping is way slower than me using python numpy bincount which returns all results to an array at once. Any ideas? Thanks. – user3152377 Aug 27 '18 at 20:13
  • 1
    You mean you want to avoid the second `for` loop? – P.W Aug 28 '18 at 05:30
  • @p-w Thanks, Yes, I would like to avoid this loop for(const auto& i: freqOfv) { if(N == i.second) std::cout << "The value " << i.first << " occurs " << N << " times." << std::endl; } – user3152377 Aug 28 '18 at 08:36
  • 1
    From the vector, create a set which contains unique elements of the vector. Then in the for loop (of the code you posted in the question), iterate using the set elements, and if the count is equal to N and add the pair to `shows` vector. – P.W Aug 28 '18 at 08:58
  • @p-w I appreciated all of your help! I will edit my original question to include the answer. – user3152377 Aug 29 '18 at 05:35