LeetCode: 169 Majority Element
LEETCODE : 169. Majority Element Link : https://leetcode.com/problems/majority-element/ Problem Description : Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. For example: Example 1: Input: [3,2,3] Output: 3 Example 2: Input: [2,2,1,1,1,2,2] Output: 2 ______________________________________________________________________________ Explaination : There is pretty easy way to solve it by using count of every element and then returning the element which has count greater than n/2 . But this solution comes with a cost of O(n) time and O(n) space. There is a better solution which can do the job in O(n) time and O(1) space. Atleast we are saving some of the space. How does our space efficient algorithm works ? Basically, what we do is that we go