Contains Duplicate Problem & Solution

Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.

See the contains duplicate problem on LeetCode.

C++ Solution

The runtime complexity of the algorithm is $O(n\log{}n)$.

#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")

static const int _=[](){std::ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();

class Solution {
public:
  bool containsDuplicate(vector<int>& nums) {
    sort(nums.begin(), nums.end());

    for (int i = 0; i < nums.size() - 1; ++i) {
      if (nums[i] == nums[i + 1]) {
        return true;
      }
    }

    return false;
  }
};

Start Here

Many paths, there are. Follow yours, you must.