leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 63e847668330d6d6471e7dd5a6396914d15fae25
parent 060ee163af4024679c425f7fdd22e552d400c921
Author: AndrewLockVI <andrew.laack@gmail.com>
Date:   Wed,  5 Apr 2023 20:00:34 -0500

Used unordered set to optimize contains duplicate problem

Diffstat:
Mcontains-duplicate/a.out | 0
Acontains-duplicate/contains-duplicateV2.cpp | 50++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 50 insertions(+), 0 deletions(-)

diff --git a/contains-duplicate/a.out b/contains-duplicate/a.out Binary files differ. diff --git a/contains-duplicate/contains-duplicateV2.cpp b/contains-duplicate/contains-duplicateV2.cpp @@ -0,0 +1,50 @@ +#include <map> +#include <iostream> +#include <unordered_set> +#include <vector> + +using namespace std; + + +//Leet Ratings +// Speed Memory +//Total 135ms 69.6MB +//Beats 70.46% 39.69% + +//I switched from using a map with keys and vals +//to using an unordered_list and it is way faster. +//It is almost 50ms faster than before! + +bool containsDuplicate(vector<int>& nums) { + + unordered_set <int> us; + + bool dupe = false; + for(int i = 0; i < nums.size() ; ++i){ + + if(us.find(nums[i]) != us.end()){ + dupe = true; + break; + } + + us.insert(nums[i]); + } + + return dupe; + +} + + + + + + + + + + + +int main(){ +vector<int> input = {0 , 5 , 2, 4, 5 , 8}; +cout << boolalpha << containsDuplicate(input) << endl; +}