Results 1 to 6 of 6

Thread: how to find items that occur more than once in a vector

  1. #1
    Join Date
    Sep 2010
    Posts
    75

    how to find items that occur more than once in a vector

    I was trying to write a program for a vector which usually gives a list of value that will be going to occur more than once in an individual element. Code:
    Code:
    std::vector<int> v2;
    vec2[0]=6; vec2[1]=6;here 6 is coming twice. I want to ask if if I make a double loop 
    std::vector<int> oc;
    { for (k=0; k<v2.size(); k++) {
       for (l=0; l<v2.size(); l++) {
          if(v2[k] == v2[l]){
             oc.push_back(v2[k]);
          } the output will be 
    oc[0]=6;
    oc[1]=6;
    oc[2]=6;
    oc[3]=6;
    I had got the value which had being repeated but I want the unique list. I there any way to do so.

  2. #2
    Join Date
    Apr 2009
    Posts
    488

    Re: how to find items that occur more than once in a vector

    If you are not willing to store the duplicate then you must use std::set which act as a container and will only store the value which is unique. I’m not confirmed but there is an library function which usually gives the value which will going to occur once in an particular vector or there might be a function which will going to take only those element which come twice and that function will sort it and returned only those value which is unique.

  3. #3
    Join Date
    May 2009
    Posts
    539

    Re: how to find items that occur more than once in a vector

    you can try this hopefully this will going to help you, below is the code:
    Code:
    for (k=0; k<v2.size(); k++) {
       for (l=0; l<v2.size(); l++) {
          if(k != l && v2[k] == v2[l]){ // this will going to remove the element and keep the unique element.
           z = 0;
             for (x=0; x<oc.size(); x++) {
                if(oc[x] == v2[k]){ z++; }
             }
             if(z == 0) { oc.push_back(v2[k]); }
    this will surely help you a bit.

  4. #4
    Join Date
    Mar 2010
    Posts
    154

    Re: how to find items that occur more than once in a vector

    Searching and processing the element which is duplicated is really a big task in the programming. It’s really thought is c programming but usually c++ provide a algorithm to do so you just need to ass the element in the map<entry, int> after that count will come. It will going to compile your program and then ass the build in map with it and after that it will going the count the items that it had come up with. It is necessary to use map in your program.

  5. #5
    Join Date
    May 2009
    Posts
    529

    Re: how to find items that occur more than once in a vector

    You can simply do this by making use of map operator in your program. I had made the code simple as the value count will be zero at time when it had being initialized.
    Code:
    #include <vector>
    #include <map>
    int main()
    {
        std::vector<int> v3;
        std::map<int, int> CountMap;
        for (auto qw = v3.begin(); qw != v3.end(); ++qw)
            CountMap[*qw]++;
     for (auto qw = CountMap;begin(); qw != CountMap.end(); ++qw)
            if (qw->second > 1)
                cout << "Duplicate " << qw->first << end;
    }

  6. #6
    Join Date
    May 2009
    Posts
    543

    Re: how to find items that occur more than once in a vector

    Well it is not related to this. I’m looking for the program which is used for bitset which will go to tell the number of instances had being created. The data structure is not much large it might be between 0 and 5 which is usually stored in an array. I’m just searching it just to get some knowledge about it. I think so I should but the data in the bitset first where I can able to read the data then enter the data in the different vector. I think so that there is nothing difference in between the map and bitwise according to their algorithm.

Similar Threads

  1. how to fast find items in hidden chronicles
    By Koppolu in forum Video Games
    Replies: 7
    Last Post: 06-02-2012, 01:49 PM
  2. Replies: 3
    Last Post: 25-01-2012, 05:14 PM
  3. Replies: 3
    Last Post: 17-01-2011, 12:58 PM
  4. Crash occur while launching Logic Pro 9.1
    By Corry in forum Operating Systems
    Replies: 3
    Last Post: 13-01-2010, 07:19 PM
  5. Email - Cannot delete items from Deleted Items folder
    By HELLIAN in forum Windows Vista Mail
    Replies: 2
    Last Post: 22-05-2008, 01:13 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,009,905.52868 seconds with 17 queries