Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,874 questions

51,798 answers

573 users

How to check whether a vector contains unique values in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
 
bool hasUniqueValues(const std::vector<int>& vec) {
    for (size_t i = 0; i < vec.size(); i++) {
        for (size_t j = i + 1; j < vec.size(); ++j) {
            if (vec[i] == vec[j]) {
                return false; // Duplicate found
            }
        }
    }
    return true; // All values are unique
}
 
int main() {
    std::vector<int> vec = {1, 8, 9, 0, 3, 4, 6};
 
    if (hasUniqueValues(vec)) {
        std::cout << "The vector contains unique values.\n";
    } else {
        std::cout << "The vector contains duplicates.\n";
    }
 
    return 0;
}
 
 
 
/*
run:
 
The vector contains unique values.
 
*/

 



answered Mar 28, 2025 by avibootz
edited Mar 28, 2025 by avibootz
...