Search code examples
c++vectorduplicatesuniquedigits

least number of digits without duplicates


I am a C++ noob. I have a list of numbers that I put into a Vector. All numbers are 9 digit integers and are unique. I want to know what is the least amount of digits (starting from the right) that can be used to uniquily identify each number in the set. right now there are only 6 numbers, but the list could potentially grow into the thousands. I have posted my code thus far (not working.)

EDIT output is the following...

digit is 1
digit is 1
digit is 1

RUN FINISHED; exit value 0; real time: 0ms; user: 0ms; system: 0ms

This is mostly a learning exercise. Please be generous and explicit with your comments and solutions.

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <cstdlib>
#include <algorithm>

using namespace std;

int main() {

    //declare stream variable and load vector with values 
    ifstream myfile("mydata.txt");
    vector<int> myVector;
    int num;
    while (myfile >> num) {
        myVector.push_back(num);
    }



    //sort and squack if there is a duplicate.  
    std::sort(myVector.begin(), myVector.end());
    for (int i = 0; i < (myVector.size() - 1); i++) {
        if (myVector.at(i) == myVector.at(i + 1)) {
            printf("There are duplicate student numbers in the file");
            exit(EXIT_FAILURE);
        }

    }
    //if it get here, then there are no duplicates of student numbers








    vector<int> newv;
    int k = 1;
    bool numberFound = false;
    bool myflag = false;

    while (numberFound == false) {
        //loop  through original numbers list and add a digit to newv.  
        for (int j = 0; j < myVector.size(); ++j) {
            newv.push_back(myVector.at(j) % (10^k));
        }
        sort(newv.begin(), newv.end());
        for (int i = 0; i < (newv.size() - 1); i++) {
            if (newv.at(i) == newv.at(i + 1)) {
                //there is a duplicate for this digit. Set flag.  
                myflag = true;
            }
            if (myflag == false) {
                numberFound = true;
                cout << "digit is " << k << endl;
            } else {
                k++;
            }

        }

    }






    //    for (int i = 0; i < myVector.size(); i++) {
    //        cout << "||" << myVector.at(i) << "||" << endl;
    //    }
    //
    //    for (int i = 0; i < newv.size(); i++) {
    //        cout << "---" << newv.at(i) << "---" << endl;
    //    }





    return 0;
}

Solution

  • Check the below code.

    #include <iostream>
    #include <vector>
    #include <fstream>
    #include <string>
    #include <cstdlib>
    #include <algorithm> 
    #include <math.h>
    using namespace std;
    
    int main() {
    
    //declare stream variable and load vector with values
    ifstream myfile("mydata.txt");
    vector<int> myVector;
    int num;
    while (myfile >> num) {
        myVector.push_back(num);
    }
    //sort and squack if there is a duplicate.
    std::sort(myVector.begin(), myVector.end());
    for (int i = 0; i < (myVector.size() - 1); i++) {
        if (myVector.at(i) == myVector.at(i + 1)) {
            printf("There are duplicate student numbers in the file");
            exit(EXIT_FAILURE);
        }
    }
    //if it get here, then there are no duplicates of student numbers
    vector<int> newv;
    int k = 1;
    bool numberFound = false;
    bool myflag = false;
    int p = 1;
    while (numberFound == false) {
        //loop  through original numbers list and add a digit to newv.
        newv.clear();
        p = p * 10;
        for (int j = 0; j < myVector.size(); ++j) {
            newv.push_back(myVector[j] % p);
        }
        sort(newv.begin(), newv.end());
         myflag = false;
        for (int i = 0; i < (newv.size() - 1); i++) {
    
            if ( newv[i] == newv[i+1]) {
                //there is a duplicate for this digit. Set flag.
                myflag = true;
                break;
            }
        }
        if (myflag == true){
            k ++;
        }else{
            numberFound = true;
                cout << "digit is " << k << endl;
                break;
        }
    }
    return 0;
    }
    

    Sample Input:

    123451789
    123456687
    125456789
    123456780
    

    Output:

    digit is 4