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,872 questions

51,796 answers

573 users

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

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <unordered_map>

bool containsUniqueValues(const std::vector<int>& vec) {
    // Create a map to store the frequency of each number
    std::unordered_map<int, int> frequency;

    // Count occurrences of each number in the array
    for (int num : vec) {
        frequency[num]++;
    }

    // Check if any number appears only once
    for (const auto& entry : frequency) {
        if (entry.second == 1) {
            return true; // Found a unique value
        }
    }

    return false; // No unique value found
}

int main() {
    // Example array
    std::vector<int> vec = {1, 2, 3, 4, 2, 1, 5, 3, 5, 6};

    if (containsUniqueValues(vec)) {
        std::cout << "The vector contains unique values.\n";
    } else {
        std::cout << "The vector does not contain any unique values.\n";
    }

    return 0;
}



/*
run:

The vector contains unique values.

*/

 



answered Mar 28, 2025 by avibootz
...