Majority Element, algorithm design big job 1.py

Source: Internet
Author: User

Majority Element

Find majority element;

Input:an array a[1 to n] of elements;

Output:the majority element if it exists;otherwise none;

1.Majority Element:

A majority element in an array a[] of size n was an element, appears more than N/2 times (and hence there are at e such element).

The majority element is the element, which occurs more than half of the size of the array. This means is the majority element occurs more than all other elements combined or if you count the number of times, Maj ority element appears, and subtract the number of all other elements, you'll get a positive number.

So if you count the number of some element, and subtract the number of any other elements and get the number 0, then your Original element can ' t be a majority element.

2,algorithm:

There are variables, counter and possible element. Iterate the stream, if the counter is 0 your overwrite the possible element and initialize of the counter, if the number is T He same as possible element-increase the counter, otherwise decrease it.

3, Pseudocode:

X<--candidate (1);

count<--0;

for J<--1 to N;

If A[j]=x then Count<--count + 1;

End for;

If COUNT>[N/2] then return x;

Else return none;

Candidate (m):

j<--m,x<--a[m],count<--1;

While J<n and count>0:

j<--j+1;

If a[j]= X then count<--count+1;

else Count <--count+1;

End while;

If J=n then return x;

else return candidate (j+1);

Source Code:

#Python Code

def majority_element (arr):

Counter, possible_element = 0, None

For I in Arr:

If counter = = 0:

Possible_element, counter = i, 1

elif i = = possible_element:

Counter + = 1

Else

Counter-= 1

Return possible_element

eg:arr=[2,5,3,5,6,8,5,5]

Start:possible_element=none

Counter=0

When i=2 counter=1 possible_element=2

When I=5 counter=0 possible_element=2 # i!=possible_element, counter-1

When I=3 counter=1 possible_element=3

When I=5 counter=0 possible_element=3 # i!=possible_element, counter-1

When I=6 counter=1 possible_element=6

When I=8 counter=0 possible_element=6 # i!=possible_element, counter-1

When I=5 counter=1 possible_element=5

When I=5 counter=2 possible_element=5

Return possible_element

Majority Element, algorithm design big job 1.py

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.