-4

I am taking an introductory level C++ class. I have to write a boolean function that checks for duplicates along a vectors and returns true and false if no duplicates

#include<iostream>
#include<vector>
using namespace std;
bool has_duplicates(const vector <int> &v);


int main() {

    vector<int> Vec(8);
    Vec = { 20, 30, 40, 50, 10, 20, 5, 6 };
    has_duplicates(Vec);
    return 0;
}
bool has_duplicates(const vector<int>& v) {
    bool duplicatefound = false;
    for (int i = 0; i < 8; i++) { // Check each other number in the array
        for (int j = i; j < 8; j++) { // Check the rest of the numbers 
            while (j != i) {// Makes sure don't check number against itself 
                if (v[i] == v[j]) {
                    cout << "duplicate found" << endl;
                    duplicatefound = true;
                    break;
                }
            }
        }
    }
    return duplicatefound; // Reset the boolean after each number entered has been checked
}
  • 2
    Can you tell me the thing you have tried? We want to help you. but we don't from 0. share your idea. – Sungguk Lim Dec 11 '15 at 03:32
  • 3
    Stack Overflow is a Q&A site. What is your **Q**uestion? – Pang Dec 11 '15 at 03:35
  • first start to create an array with some duplicate values. Then loop over the array. Inside the previous loop try to create an other loop and look for a way to return true if you find duplicate value – Olivier Pellier-Cuit Dec 11 '15 at 03:37
  • Possible duplicate of [More elegant way to check for duplicates in C++ array?](http://stackoverflow.com/questions/4003584/more-elegant-way-to-check-for-duplicates-in-c-array) – markasoftware Dec 11 '15 at 03:41
  • One problem (minus the syntax errors) you have that you allow i to equal j in the following `if (v[i] == v[j]) {` This will make has_duplicates always return true. – drescherjm Dec 11 '15 at 04:44
  • i inserted a while statement while (j != i) before that let me post an update program – Ronnie Fedha Dec 11 '15 at 04:50
  • Use an `if` instead of `while` for that or combine the 2 conditionals in one using && – drescherjm Dec 11 '15 at 04:59
  • @drescherjm it worked with the while i didn't put the function in a cout statement hence the issue i was getting – Ronnie Fedha Dec 11 '15 at 05:01
  • With the while you should have had an infinite loop when v[i] != v[j] since neither i nor j were changing in the while (i != j) {} loop. – drescherjm Dec 11 '15 at 05:03
  • i introduced the if loop instead while actually – Ronnie Fedha Dec 11 '15 at 05:11

2 Answers2

0

There is a very simple solution to your problem:

  1. Iterate through your array, using a loop: for(int i=0; i < array_length; i++)
  2. Within this loop, make use of another loop, to check whether any value prior to the one, to which i is pointing, is equal to your_array[i]. If this is the case you have found a duplicate and can return true, else the loops will just run until the end of the array is reached and then you can return false. Thus your entire function would be something like:

    bool contains_duplicates(int array[], int len) {
        for(int i=0; i < len; i++) {
            for(int j=0; j < i; j++) {
                if(array[j]==array[i]) {
                    return true;
                }
            }
        }
    
        return false;
    }
    

Hope I could help, cheers!
lindebear

lindebear
  • 167
  • 10
-1

You can use templates to extend the type contained on vector like this. This solution reduces the algorithm complexity to O(n log n) which is better than O(n^2) from your nested loop, it doesn't mean it will be faster always but it make a difference on large vectors.

template< typename T>
bool hasDuplicates(std::vector<T> vect) {
    std::sort(vect.begin(), vect.end());
    T last;
    typename vector<T>::iterator it;
    for(it = vect.begin(); it < vect.end(); it++) {
        if (vect.begin() != it) {
            if(last == *it) {
                return true;
            }
        }
        last = *it;
    }
    return false;
}