Vector to vector without repeating elements

Basically, I have a vector with 10 elements, and I need to create another vector with 6 non-repeating elements, taken randomly from the first vector. Kind of like a 6-combination. The thing is, though, that I cannot successfully exclude the repeating elements and I always get repeated elements in my 6-element vector.

I tried several things: nested loop with an iterator to check content of the vector, I tried removing the element, which I just inserted into the second element - I get crashes or repeated elements in the end.

What is the best approach? Something I haven't tried yet is using binary search.

Here are some attempts I did, which did not work. I do understand why they do not work, but not well enough to improve these solutions and make it work.
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
36
37
38
39
40
41
42
//One try - vec3 is a vector if integers
    vector<int>::iterator iter;
    srand(time(NULL));
    vector<int> sixComb;
    for(int i=0; i<6; i++)
    {
        int k=1+rand()%10;
        int numb = vec3[k];
        if (sixComb.size() == 0)
        {
            sixComb.push_back(numb);
        }
        for (iter=sixComb.begin(); iter != sixComb.end(); ++iter)
        {
            if (numb == *iter) break;
            if (numb != *iter) sixComb.push_back(numb);
        }
    }
//Second try
    vector<int>::iterator iter;
    srand(time(NULL));
    vector<int> sixComb;
    for(int i=0; i<6; i++)
    {
        int k=1+rand()%10;
        int numb = vec3[k];
        do
        {
            sixComb.push_back(numb);
        }
        while (numb!=sixComb[i]);

    }
//Three - I tried deleting the element I have just inserted
    for(int i=0; i<6; i++)
    {
        int k=1+rand()%10;
        int numb = vec3[k];
        sixComb.push_back(numb);
        vec3.erase (vec3.begin()+k);

    }
1
2
3
4
5
6
7
8
9
bool is_member(element, container){
   return find( container.begin(), container.end(), element ) not_eq container.end();
}

repeat(6){
   while( is_member( vec[K], sixComb )
      K = give_me_other();
   sixComb.push_back(vec[K]);
}
Last edited on
What happens if there are not 6 non-repeating elements in your original vector?

Can you sort the the original vector, or must the order be maintained?

By the way, I myself would remove the call to srand() until you have the program working for one set of numbers.






The elements in my original vector are fixed and there are NO repeated elements in there.
The order does not matter, the resulting 6-element vector would eventually be sorted.

Copy all the elements of the original vector into a new vector. Randomize the new vector. Truncate it to N elements. Sort.
SOLVED

@Duoas how would that assure me that there will be no repeated elements in the new vector?

Edit: I think I understand - because of the fact that I just use the original vector's content once, without copying repeatedly differeny, random elements N times.
Last edited on
Each time, before you copy over one element, loop through your vector and make sure it doesnt exist there already.
Last edited on
Sorry, I assumed that your source vector has no duplicates.
Topic archived. No new replies allowed.