leetcode

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

commit 5dc749caa668be5a75e54b0ed1fab700b7eaaef2
parent 73533f550f6857ffebcb040ce77303779f0b49fe
Author: AndrewLockVI <andrew.laack@gmail.com>
Date:   Fri,  7 Apr 2023 10:09:08 -0500

Majority element problem with hashmap

Diffstat:
Amajority-element/a.out | 0
Amajority-element/majority.cpp | 50++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 50 insertions(+), 0 deletions(-)

diff --git a/majority-element/a.out b/majority-element/a.out Binary files differ. diff --git a/majority-element/majority.cpp b/majority-element/majority.cpp @@ -0,0 +1,50 @@ +#include <iostream> +#include <map> +#include <vector> + + +//Leet Ratings +// Speed Memory +//Total 21ms 19.6MB +//Beats 57.4% 37.39% + + + +using namespace std; + +int majorityElement(vector<int>& nums) { + map <int, int> num_map; + for(int i = 0; i < nums.size() ; ++i){ + if(num_map.find(nums[i]) != num_map.end()){ + num_map[nums[i]] += 1; + } + else{ + num_map.insert({nums[i] , 1}); + } + } + + //Get most common number + + int largest_val = nums[0]; + int largest_num = 0; + for(auto i = num_map.begin() ; i != num_map.end() ; ++i){ + if(i -> second > largest_num){ + largest_num = i ->second; + largest_val = i -> first; + + } + } + return largest_val; +} + + + + +int main(){ + vector<int> nums = {0, 2, 2, 4, 5, 6, 7, 2 , 2, 2, 2}; + cout << majorityElement(nums) << endl; + + + + return 0; +}