-1

I have tried making this function, but with no success. I am just starting c++ and can not seem to figure this out.

vector<string> findUniqueWords(vector<string> vardi){
    vector<string> unikVardi;
    unikVardi.push_back(vardi[0]);
    for (int i = 1; i < vardi.size(); i++){
        for(int k = 0; k < unikVardi.size(); k++){
            if (vardi[i] != unikVardi[k]){
                unikVardi.push_back(vardi[i]);
            }
        }

     return unikVardi;
    }

}
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
Melon987
  • 49
  • 5

5 Answers5

0

you must write something like that:

vector<string> findUniqueWords(vector<string> vardi){
vector<string> unikVardi;
unikVardi.push_back(vardi[0]);
for (int i = 1; i < vardi.size(); i++){
    bool unique = true;
    for(int k = 0; k < unikVardi.size(); k++){
        if (vardi[i] == unikVardi[k]){
           unique  = false;
        }
    }
    if(unique)    unikVardi.push_back(vardi[i]);

}
return unikVardi;

}

Aram Gevorgyan
  • 2,175
  • 7
  • 37
  • 57
0

The idea is to push the elements only once if they do not exist in the resultant words list. You can use a loop, std::count, std::find, etc. to verify the non-existence of the elements. Or, you can also use std::unique directly.

With a loop, it would be something like this (live):

using Words = std::vector<std::string>;

Words findUniqueWords( const Words& words )
{
    Words uniqueWords;
    uniqueWords.push_back( words[0] );

    for ( int i = 1; i < words.size(); ++i )
    {
        bool isAdded = false;
        for ( int j = 0; j < uniqueWords.size(); ++j )
        {
            if ( words[i] == uniqueWords[j] )
            {
                isAdded = true;
                break;
            }
        }

        if ( !isAdded )
        {
            uniqueWords.push_back( words[i] );
        }
    }

    return uniqueWords;
}

Here's an example with std::count (live):

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

using Words = std::vector<std::string>;

Words findUniqueWords( const Words& words )
{
    Words uniqueWords;
    uniqueWords.push_back( words[0] );

    for ( int i = 1; i < words.size(); ++i )
    {
        if ( std::count( uniqueWords.cbegin(), uniqueWords.cend(), words[i] ) == 0 )
        {
            uniqueWords.push_back( words[i] );
        }
    }

    return uniqueWords;
}

int main()
{
    const Words words { "abc", "xyz", "abc", "xyz", "jkl" };

    const auto result = findUniqueWords( words );
    for ( const auto& word : result )
    {
        std::cout << word << '\n';
    }

    return 0;
}

Output:

abc
xyz
jkl

Also, you need to take care of the case when the input words is an empty list.

Relevant thread: Why is "using namespace std;" considered bad practice?

Azeem
  • 11,148
  • 4
  • 27
  • 40
0

It should return with one element of unikVardi with the first element of vardi

You are pushing the first element with these two lines:

vector<string> unikVardi;
unikVardi.push_back(vardi[0]);

Now You are comparing with these two vector

 for (int i = 1; i < vardi.size(); i++){

    // unikVardi.size() == 1

     for(int k = 0; k < unikVardi.size(); k++){
        if (vardi[i] != unikVardi[k]){
          unikVardi.push_back(vardi[i]);
     }
 }
 return unikVardi;

After one iteration it will go back to the calling station. :-P

See with main():

#include <iostream>
#include <bits/stdc++.h>
#include <string>

using namespace std;

vector<string> findUniqueWords(vector<string> vardi){


    vector<string> unikVardi;
    unikVardi.clear();


    unikVardi.push_back(vardi[0]);


    for (int i = 1; i < vardi.size(); i++) {
        for(int k = 0; k < unikVardi.size(); k++) {

        cout<<vardi[i]<<" Outside:  "<<unikVardi[k]<<endl;
            if (vardi[i] != unikVardi[k]){
                cout<<vardi[i]<<" "<<unikVardi[k]<<endl;
                unikVardi.push_back(vardi[i]);
            }
        }

     return unikVardi;
    }

}

int main()
{
    vector<string> g1, g2;
    string str = "This";

    for (int i = 1; i <= 5; i++)
        g1.push_back(str);

    g2 = findUniqueWords(g1);

    for (int i = 0; i < g2.size(); i++)
        cout<<g2.size()<<endl;
    return 0;
}

It will give you just This because it has only one element. This element is equeal to vardi[1] second element. So, it will not go to the if loop. Back to main() with one element.

I hope you will now understand what is occuring in your function.

Mr. Perfectionist
  • 2,605
  • 2
  • 24
  • 35
0

The std::unique link actually has an example to do exactly this, using std::sort,

std::vector<std::string> findUniqueWords(std::vector<std::string> vardi)
{
    std::sort(vardi.begin(), vardi.end());
    vardi.erase(std::unique(vardi.begin(), vardi.end()), vardi.end());
    return vardi;
}

Using the range-v3 library, this becomes even easier,

std::vector<std::string> findUniqueWords(std::vector<std::string> vardi)
{
    return vardi 
           | ranges::move 
           | ranges::actions::sort 
           | ranges::actions::unique;
}
cigien
  • 57,834
  • 11
  • 73
  • 112
0

Smart guys already designed in the standard library all what you may need. If you want to successfully study C++ you should read manuals, they often give good examples, std::unique

vector<string> findUniqueWords(vector<string> vardi){
    std::sort(vardi.begin(), vardi.end());
    auto last = std::unique(vardi.begin(), vardi.end());
    vardi.erase(last, vardi.end());
    return vardi;
}
273K
  • 29,503
  • 10
  • 41
  • 64