Leetcode Note: Majority Element

Source: Internet
Author: User

Leetcode Note: Majority Element

I. Description

Given an array of size n, find the majority element. The majority element is the element that appears more? n/2 ?Times.

You may assume that the array is non-empty and the majority element always exist in the array.

Ii. Question Analysis

Given an array containingnElements. Find a primary element. The number of times this element appears in the array is more than the number of times that other elements appear, that is, the number of these elements is greatern/2.

The initial thought is to sort the array. The most intermediate element of the array is the primary element, but the algorithm complexity is generally required:O(nlogn)If secondary memory is allowed, a new array can be created to store the number of times different element values are stored in the array. In this way, you only need to traverse the original array once, then, traverse the array of record times to find the main element. The algorithm complexity isO(n).

An Efficient Method only needs to traverse the array once. We know that the number of occurrences of the primary element is greater than that of other elements. Therefore, we only need to use two variables:candidateAndcountThese two variables record the elements.candidateValue,countRecorded ElementcandidateMore times than other elements.

Traverses the array and encounters an element associated with the current recordcandidateSame element value,++countOtherwisecountOffset once,--count. WhencountChange0ChangecandidateThe pixel value of the current traversal.

Number of times the primary element is offset after the array is traversedcountIf the value is greater than zerocountChange0It is replaced by other array elements, socandidateIt will only be the primary element.

Iii. Sample Code

class Solution {public:    int majorityElement(vector
  
   & nums) {        int candidate = 0, count = 0;        for (int i = 0; i < nums.size(); ++i)        {            if (count == 0)            {                candidate = nums[i];                ++count;            }            else            {                if (candidate == nums[i])                    ++count;                else                    --count;            }        }        return candidate;    }};
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.