Skip to content

Instantly share code, notes, and snippets.

@AmnesiacSloth
Created September 11, 2024 02:49
Show Gist options
  • Save AmnesiacSloth/0417d8f4d1c3e58f395a9a585a78c7ea to your computer and use it in GitHub Desktop.
Save AmnesiacSloth/0417d8f4d1c3e58f395a9a585a78c7ea to your computer and use it in GitHub Desktop.

Duplicate Integer 📗

Given an integer array nums, return true if any value appears more than once in the array, otherwise return false.

Example 1

Input: nums = [1, 2, 3, 3]

Output: true

Example 2

Input: nums = [1, 2, 3, 4]

Output: false
class Solution {
public:
bool hasDuplicate(vector<int>& nums) {
unordered_set<int> encountered;
for (int i = 0; i < nums.size(); i++) {
if (encountered.count(nums[i])) {
return true; // duplicate identified
}
encountered.emplace(nums[i]);
}
return false; // no duplicates found
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment