简体   繁体   中英

How to remove duplicates from vector by iteratting though?

I'm doing an assignment for my computer engineering class where we have to remove duplicates from a vector. I found solutions elsewhere, but I can't figure out how to iterate through without including the algorithm library

#include <vector>

using namespace std;

vector<int> deleteRepeats(const vector<int>& nums); // Declaration
void print(const vector<int>& nums);

int main() {
    vector<int> nums;
    vector<int> res;
    // Test 1
    nums = {1, 2, 3, 3, 2};
    res = deleteRepeats(nums);
    print(res); // Should print 1, 2, 3
    // Test 2
    nums = {-1, 2, 4};
    res = deleteRepeats(nums); 
    print(res); // Should print -1, 2, 4
    // Test 3
    nums = {42, 42, 42,};
    res = deleteRepeats(nums); 
    print(res); // Should print 42
    // Test 4
    nums = {};
    res = deleteRepeats(nums); 
    print(res); // Should print a blank

    return 0;
}

vector<int> deleteRepeats(const vector<int>& nums) {
    vector<int> res;
    bool foundRepeat;
    //my code here

    return res;
}

void print(const vector<int>& nums) {
    for (int i = 0; i < nums.size(); i++)
        cout << nums[i] << " ";
    cout << endl;
}

The most simple way to do this is to create a second std::vector then iterate through std::vector that you want to remove duplicate from and add to second std::vector if the item doesn't exist in the second std::vector

#include <stdio.h>
#include <vector>
bool DoesVectorContain(const std::vector<int> &vect, int item)
{
    for (int i = 0; i < vect.size(); i++)
    {
        if (vect.at(i) == item)
            return true;
    }
    return false;
}
std::vector<int> RemoveDuplicate(const std::vector<int> &vect)
{
    std::vector<int> newVect;
    for (int i = 0; i < vect.size(); i++)
    {
        //add to newVect if vect.at(i) doesn't exist in newVect
        if (!DoesVectorContain(newVect, vect.at(i)))
            newVect.push_back(vect.at(i));
    }
    return newVect;
}
int main()
{
    std::vector<int> vect = {0, 2, 4, 4, 3, 4, 4, 8, 8, 10, 12};
    std::vector<int> newVect = RemoveDuplicate(vect);
    for (int i = 0; i < newVect.size(); i++)
        printf("%d\n", newVect.at(i));

    return 0;
}

this prints out

0
2
4
3
8
10
12

You can use an unordered_set object, which allows insertion of an element only if it doesn't exist. After the insertion you can convert the set into a vector.

This solution has an average complexity of O(N) (but not in the worst case)

#include <vector>
#include <unordered_set>

std::vector<int> deleteRepeats(const std::vector<int>& nums) {
    std::unordered_set<int> uniq_set;

    for (const auto& element : nums) {
        uniq_set.insert(element);
    }

    return std::vector(uniq_set.begin(), uniq_set.end());
}

So, the requirement is to not use the algorithm library and do it manually.

Also no problem, because your teacher gave you already a strong hint by writing:

vector<int> deleteRepeats(const vector<int>& nums) {
    vector<int> res;
    bool foundRepeat;
    //my code here

    return res;
}

You have an input vector nums and a result vector res . Obviously you need to copy values from the input vector to the resulting vector. But no duplicates.

The solution is to

  • iterate all numbers in the input vector nums
  • and then compare each number with all numbers in the resulting vector res
  • and if youd could not find a repeat ( foundRepeat ), copy the value into the resulting vector.

You need 2 loops. The outer loop will read all values from the input vector nums . Each of this value will be compared with all existing values in the res vector. For this we create an inner llop and iterate over all existing values in the res vector. If the number from the outer loop is equal to the value from the inner loop, then we have obviously a duplicate. We will not add the value. If we do not have a duplicate, then we add the number from the outer loop to the res vector.

This could then look for example like the below:

#include <iostream>
#include <vector>

using namespace std;

vector<int> deleteRepeats(const vector<int>& nums); // Declaration
void print(const vector<int>& nums);

int main() {
    vector<int> nums;
    vector<int> res;
    // Test 1
    nums = { 1, 2, 3, 3, 2 };
    res = deleteRepeats(nums);
    print(res); // Should print 1, 2, 3
    // Test 2
    nums = { -1, 2, 4 };
    res = deleteRepeats(nums);
    print(res); // Should print -1, 2, 4
    // Test 3
    nums = { 42, 42, 42, };
    res = deleteRepeats(nums);
    print(res); // Should print 42
    // Test 4
    nums = {};
    res = deleteRepeats(nums);
    print(res); // Should print a blank

    return 0;
}

vector<int> deleteRepeats(const vector<int>& nums) {
    vector<int> res;
    bool foundRepeat;
    //my code here

    // Iterate over all nums
    for (unsigned int i = 0; i < nums.size(); ++i) {

        // Get current num at index i
        int num = nums[i];

        // Here we will indicate, if we found a duplicate
        foundRepeat = false;

        // Now search, if this num is already in the result vector
        for (unsigned int k = 0; (k < res.size() && !foundRepeat); k++) {

            // This is the number in the res vector at the current index
            int numInRes = res[k];

            // Check for a repeating (duplicate value)
            if (numInRes == num) {

                // Remeber that we found a duplicate and also stop the loop
                foundRepeat = true;
            }
        }
        // If no duplicate has been found, then add the num to the result
        if (!foundRepeat) res.push_back(num);
    }
    return res;
}

void print(const vector<int>& nums) {
    for (int i = 0; i < nums.size(); i++)
        cout << nums[i] << " ";
    cout << endl;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM