Skip to content
Quiz · Algorithms and Complexity · lesson 2 of 9

Quiz — which complexity

What is the worst-case time complexity of has_duplicate for a vector of n elements?

C++
#include <vector>

bool has_duplicate(const std::vector<int>& v) {
    for (std::size_t i = 0; i < v.size(); ++i) {
        for (std::size_t j = i + 1; j < v.size(); ++j) {
            if (v[i] == v[j]) {
                return true;
            }
        }
    }
    return false;
}