Contains Duplicate Solutions in C++
Number 217
Difficulty Easy
Acceptance 56.0%
Link LeetCode
Other languages Go
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/contains-duplicate/// Author : Hao Chen// Date : 2015-06-11class Solution {public:bool containsDuplicate(vector<int>& nums) {unordered_map<int, bool> m;for (auto item : nums) {if (m.find(item) != m.end()) return true;m[item]=true;}return false;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/contains-duplicate//// Author : liuyubobobo/// Time : 2017-01-18#include <iostream>#include <unordered_set>#include <vector>using namespace std;/// Using HashTable/// Time Complexity: O(n)/// Space Complexity: O(n)class Solution {public:bool containsDuplicate(vector<int>& nums) {unordered_set<int> record;for( int i = 0 ; i < nums.size() ; i ++ )if( record.find( nums[i] ) == record.end() )record.insert( nums[i] );elsereturn true;return false;}};void print_bool(bool res){cout << (res ? "True" : "False") << endl;}int main() {vector<int> vec = {0, 0, 1};print_bool(Solution().containsDuplicate(vec));return 0;}