contains-duplicateV2.cpp (762B)
1 #include <map> 2 #include <iostream> 3 #include <unordered_set> 4 #include <vector> 5 6 using namespace std; 7 8 9 //Leet Ratings 10 // Speed Memory 11 //Total 135ms 69.6MB 12 //Beats 70.46% 39.69% 13 14 //I switched from using a map with keys and vals 15 //to using an unordered_list and it is way faster. 16 //It is almost 50ms faster than before! 17 18 bool containsDuplicate(vector<int>& nums) { 19 20 unordered_set <int> us; 21 22 bool dupe = false; 23 for(int i = 0; i < nums.size() ; ++i){ 24 25 if(us.find(nums[i]) != us.end()){ 26 dupe = true; 27 break; 28 } 29 30 us.insert(nums[i]); 31 } 32 33 return dupe; 34 35 } 36 37 38 39 40 41 42 43 44 45 46 47 int main(){ 48 vector<int> input = {0 , 5 , 2, 4, 5 , 8}; 49 cout << boolalpha << containsDuplicate(input) << endl; 50 }