user3475204
user3475204

Reputation: 1

To find duplicate in 2D Vector C++

I wrote a program to find duplicate in 2D vector C++. I have checked only one field, that is name field. Is this efficient way to restrict duplicate inputs?

typedef vector<vector<string>> table_t;

    table_t myvec = {{"DINU","56","66","45","78","76"},
                     {"HARI","56","78","54","85","62"},
                     {"JAMES","65","34","54","64","98"},
                     {"HEARTLY","76","76","87","34","76"},
                     {"PETER","67","78","87","34","53"}};
int main(int argc, char *argv[])
{
    if(argc!=7)
    {
        cout<<"Enter 7 Fields"<<endl;
        exit(0);
    }

    string name = argv[1];
    upperto(name);

    for(size_t i=0;i<myvec.size();i++)
    {
        if(name == myvec[i][0])
        {
            id++;
        }
    }

    if(id==0)
    {
        myvec.push_back({name,argv[2],argv[3],argv[4],argv[5],argv[6]});
    }
    else
    {
        cout<<'\n'<<"Already Exist"<<endl;
    }

}

Upvotes: 0

Views: 669

Answers (2)

DoOrDoNot
DoOrDoNot

Reputation: 1156

Just create a vector of the input and compare the whole vector :

vector<string> input;

for (int i = 0; i < argc; i++)
    input.push_back(arg[i]);

for(size_t i = 0; i < myvec.size(); i++) {
    if(input == myvec[i])
        // check here
}

Upvotes: 1

Umer Sufyan
Umer Sufyan

Reputation: 166

your approach is okay but if you want to make it more efficient, you can use hashing. It will give you result in constant time.

Upvotes: 0

Related Questions