From 46b3defea0dea874d575873c0d1f94252290a472 Mon Sep 17 00:00:00 2001 From: May-We-Code <96605037+May-We-Code@users.noreply.github.com> Date: Sat, 29 Oct 2022 10:15:27 +0530 Subject: [PATCH] Create imp-hello-ji.cpp --- imp-hello-ji.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 imp-hello-ji.cpp diff --git a/imp-hello-ji.cpp b/imp-hello-ji.cpp new file mode 100644 index 0000000..3d7f3db --- /dev/null +++ b/imp-hello-ji.cpp @@ -0,0 +1,31 @@ + httpsleetcode.comproblemsmajority-element +class Solution { + public int majorityElement(int[] nums) { + + int cur = nums[0]; + int cnt = 1; + for (int i=1;inums.length;i++){ + if(nums[i] == cur){ + cnt++; + } + else{ + cnt--; + } + if(cnt == 0){ + cur = nums[i]; + cnt = 1; + } + } + return cur; + cnt = 0; + for(int e nums){ + if (e == cur){ + cnt++; + } + } + if (cnt nums.length2){ + return cur; + } + return -1; + } +}