contains-duplicate.cpp (760B)
1 #include <map> 2 #include <iostream> 3 #include <vector> 4 using namespace std; 5 6 7 //Leet Ratings 8 // Speed Memory 9 //Total 186ms 70.8MB 10 //Beats 23.71% 29.11% 11 12 // I chose to use maps because I did not know 13 // that unordered lists exist. I will fix and 14 // see the difference. I suspect it will be 15 // margianlly faster and use far less memory. 16 17 18 bool containsDuplicate(vector<int>& nums) { 19 map <int, int> maps; 20 bool dupe = false; 21 for(int i = 0; i < nums.size() ; ++i){ 22 if(maps.find(nums[i])->second){ 23 dupe = true; 24 break; 25 } 26 maps.insert(pair(nums[i] , 1)); 27 } 28 return dupe; 29 } 30 31 int main(){ 32 vector<int> input = {0 , 5 , 2, 4, 5 , 8}; 33 cout << boolalpha << containsDuplicate(input) << endl; 34 }