Welcome to collectivesolver - Programming & Software Q&A with Examples. A website you can trust. All programs tested and work.

Contact: aviboots(AT)netvision.net.il

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Survey Junkie is the #1 place online for your audience to earn rewards for their opinion.

Managed VPS Hosting
Affordable. Secure. Your Own Virtual Private Hosting. Faster than AWS and Rackspace

Bonsai Boy of New York Bonsai Trees and Accessories
Liquid Web - Highest Level of Performance and Security with Managed Dedicated Servers

Disclosure We are a professional Programming & Software Q&A website, that receives compensation from some of the links whose we show you with Information. It means that my content may contain affiliate links.

20,687 questions

27,328 answers

573 users

How to find the smallest missing number from an unsorted vector in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <unordered_set>
#include <iterator>

int findSmallestMissingNumber(std::vector<int> const &v) {
    std::unordered_set<int> us(v.begin(), v.end());

    int index = 1;
    while (true) {
        if (us.find(index) == us.end()) {
            return index;
int main()
    std::vector<int> v = { 1, 5, 2, 7, 4, 9, 12 };
    std::cout << findSmallestMissingNumber(v);
    return 0;



answered Nov 22 by avibootz