-3

I have different type of string and I need to find out the duplicate entry in this string. Different type of string

string aStr= "1.1,1.2,1.3, 1"; //output should be duplicate entry
string aStr= "1.1,1.2,1.3"; //Ouput NO duplicate entry
string aStr= "1,2,1"; //o/p duplicate entry
string aStr = "1,2,3";//output No duplicate 

I have tried as 
std::vector < std::string > aOutString;
std::set <int> full_list;
std::set <std::string> aEntryInDouble;
int aNum1;
boost::split(aOutString, aStr , boost::is_any_of(","));
for(size_t it = 0; it < aOutString.size(); ++it)
{
if (aOutString[it].find('.') != std::string::npos)
{
 //If entry is like "1.1,1.2,1.3"
  if( !aEntryInDouble.insert(aOutString[it]).second )
  {
     aDup = false;
     break;
  }
}
 else
{
//For entry "1,2,1"
 aNum1 = std::atoi(aOutString[it].c_str());
 if(aNum1)
 {
 if( !full_list.insert(aNum1).second )
 {
 aDup = false;
 break;
 }
 }
  1. I am not able to find out the solution for entry string "string aStr= "1.1,1.2,1.3, 1"; Please help me to find out the solution.

Thanks,

erip
  • 16,374
  • 11
  • 66
  • 121
CrazyCoder
  • 772
  • 5
  • 11
  • 31
  • What makes you unable? What _specific_ problem did you encounter? – Lightness Races in Orbit Jul 15 '16 at 11:15
  • Why are you handling the `.` explicitly? The algorithm seems simple: split on commas. Create a set from the split list. If the length of the set is equal to the length of the split list, no dupes. – erip Jul 15 '16 at 11:15
  • Put here the task's text, please. For example, define the "duplicate." – Les Jul 15 '16 at 11:15
  • `string aStr= "1.1,1.2,1.3, 1"; //output should be duplicate entry` I think that either the commentary or the string are wrong. – Nicolas Jean Jul 15 '16 at 11:28
  • @NicolasJean we know this string wrong, that's why I need to handle in our code. User input like that. – CrazyCoder Jul 15 '16 at 11:31
  • @CrazyCoder Yes, but there are no duplicates in that string. – erip Jul 15 '16 at 11:33
  • @CrazyCoder if you consider that the first string is malformed, then it may be adequate to output an error. – Nicolas Jean Jul 15 '16 at 11:35
  • the meaning of this input string aStr= "1.1,1.2,1.3, 1" here, 1.1 means print 1st sub heading. 1.2 means print 2nd sub heading. 1.3 means print 3rd sub heading. now user put 1, that means users gives the command to print all but we already printed all option of 1, so no need to print and said to users printed already.. – CrazyCoder Jul 15 '16 at 12:08

1 Answers1

1

Here's an algorithm:

Split the input on commas. You will create a list of everything that is comma-delimited. You will then create a set from the list that may contain duplicates. This will remove all duplicates for you. If the list's length is equal to the set's length, there are no duplicates. Otherwise, constructing the set removed duplicates and it is shorter than the list.


Here's the code in C++. I took the tokenize from this answer and made a few modifications. Also, here's the coliru.

#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>

std::vector<std::string> split(const std::string& str, const std::string& delimiters = ",") {
    std::vector<std::string> tokens;

    // Skip delimiters at beginning.
    auto lastPos = str.find_first_not_of(delimiters, 0);
    // Find first "non-delimiter".
    auto pos = str.find_first_of(delimiters, lastPos);

    while(std::string::npos != pos || std::string::npos != lastPos) {
        // Found a token, add it to the vector.
        tokens.push_back(str.substr(lastPos, pos - lastPos));
        // Skip delimiters.  Note the "not_of"
        lastPos = str.find_first_not_of(delimiters, pos);
        // Find next "non-delimiter"
        pos = str.find_first_of(delimiters, lastPos);
    }

    return tokens;
}

bool has_dupes(const std::vector<std::string>& v) {
    std::unordered_set<std::string> s(v.cbegin(), v.cend());
    return s.size() != v.size();
}

std::string detect_duplicates(const std::string& s) {
    auto v = split(s);
    return has_dupes(v) ? "duplicates" : "no duplicates";
}

int main() {
    // dupes
    std::string dupes = "1,2,3,4,1";
    std::cout << detect_duplicates(dupes) << '\n';

    // no dupes
    std::string no_dupes = "1,2,3";
    std::cout << detect_duplicates(no_dupes) << '\n';           
}
Community
  • 1
  • 1
erip
  • 16,374
  • 11
  • 66
  • 121
  • It is running in all scenario but it is failed in std::string dupes = "1.1,2.1,3,4,1"; this string have also duplicate. – CrazyCoder Jul 15 '16 at 11:38
  • 1
    @CrazyCoder That string has no duplicates. 1.1, 2.1, 3, 4, and 1 are all different numbers... – erip Jul 15 '16 at 11:39