217. 存在重复元素
题目描述
题目链接

使用哈希集合查重
#pragma once
#include <iostream>
#include<vector>
#include<unordered_set>
using namespace std;
class Solution
{
public:
bool containsDuplicate(vector<int>& nums)
{
unordered_set<int> hashset;
for (int num : nums)
{
if (hashset.count(num) > 0)
{
return true;
}
hashset.insert(num);
}
return false;
}
};
int main()
{
int arr[] = { 1,2,3,4};
vector<int> nums;
for (int i = 0; i < sizeof(arr) / sizeof(int); i++)
{
nums.push_back(arr[i]);
}
Solution S;
bool res = S.containsDuplicate(nums);
if (res)
{
cout << "存在重复值!" << endl;
}
else
{
cout << "不存在重复值!" << endl;
}
system("pause");
return 0;
}
运行结果

提交结果
