Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Run-time complexity O(nlogn)
- // Memory complexity O(1)
- bool containsDuplicate(vector<int>& nums) {
- if (nums.empty())
- return false;
- sort(nums.begin(), nums.end());
- for (int i = 0; i < nums.size() - 1; ++i) {
- if (nums[i] == nums[i + 1]) {
- return true;
- }
- }
- return false;
- }
- // Run-time complexity O(n)
- // Memory complexity O(n)
- bool containsDuplicate(vector<int>& nums) {
- unordered_set<int> hashset;
- for (int num : nums) {
- if (hashset.count(num))
- return true;
- hashset.insert(num);
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement