diff --git a/Medium/229.MajorityElement-ll/README.md b/Medium/229.MajorityElement-ll/README.md new file mode 100644 index 0000000..45dea48 --- /dev/null +++ b/Medium/229.MajorityElement-ll/README.md @@ -0,0 +1,24 @@ +The logic behind this code is to find the majority elements in an input array `nums`. A majority element is defined as an element that appears more than `n/3` times in the array, where `n` is the length of the array. The code achieves this by iterating through the sorted array and keeping track of the count of each element. + +Here's a step-by-step explanation of the logic: + +## 1. Initial Setup: + - An empty `ArrayList` called `result` is created to store the majority elements found. + - If the length of the input array `nums` is less than 3, it's a special case because in small arrays, any element that appears once or twice is considered a majority element. In this case, the code simply adds unique elements to the `result` list (ensuring no duplicates) and returns it. + +## 2. Sorting: + - If the input array has 3 or more elements, it sorts the array in ascending order using `Arrays.sort(nums)`. Sorting the array simplifies the process of counting the occurrences of each element because identical elements will be adjacent to each other after sorting. + +## 3. Counting Elements: + - The code then iterates through the sorted array using a loop. + - It maintains a `count` variable initialized to 1 to keep track of how many times the current element appears. + - If the current element is the same as the next element (i.e., nums[i] == nums[i+1]), it increments the `count` because it has encountered another occurrence of the same element. + - If the count is greater than 1 (meaning there is a repeated element) and the current element is not the same as the next element, it means that the current element has ended its consecutive appearances. In this case, the `count` is reset to 1. + +## 4. Checking for Majority Elements: + - After counting the occurrences of each element in the sorted array, the code checks if the `count` of the current element is greater than or equal to `gt + 1`, where `gt` is calculated as `nums.length / 3`. If this condition is met, it means that the current element appears more than `n/3` times in the original unsorted array. + - When this condition is satisfied, the current element is added to the `result` list (if it's not already in the list to avoid duplicates), and the `count` is reset to 1. + +## 5. Final Result: + - Once the loop has iterated through the entire sorted array, the `result` list contains the majority elements that satisfy the condition of appearing more than `n/3` times in the input array. + diff --git a/Medium/229.MajorityElement-ll/Solution.cpp b/Medium/229.MajorityElement-ll/Solution.cpp new file mode 100644 index 0000000..794b413 --- /dev/null +++ b/Medium/229.MajorityElement-ll/Solution.cpp @@ -0,0 +1,34 @@ +#include +#include +#include + +using namespace std; + +class Solution +{ +public: + vector majorityElement(vector& nums) { + vector vec; + int n= floor(nums.size()/3)+1; + map mp; + map::iterator it; + for(int i=0;i(nums[i],1)); + else it->second=mp[nums[i]]+1; + } + for(it=mp.begin();it!=mp.end();it++){ + if(it->second>=n) + vec.push_back(it->first); + } + return vec; + } +}; + +int main() +{ + + // call the fn here + return 0; +} \ No newline at end of file diff --git a/Medium/229.MajorityElement-ll/Solution.java b/Medium/229.MajorityElement-ll/Solution.java new file mode 100644 index 0000000..787ea23 --- /dev/null +++ b/Medium/229.MajorityElement-ll/Solution.java @@ -0,0 +1,32 @@ +class Solution{ +public List majorityElement(int[] nums) { + List result = new ArrayList<>(); + if(nums.length < 3){ + int n = nums.length; + for(int i=0; i 1 && nums[i] != nums[i+1]){ + count = 1; + } + if(count >= gt+1){ + if(!result.contains(nums[i])){ + result.add(nums[i]); + count = 1; + } + } + } + return result; + } +} \ No newline at end of file