169. Majority Element

Source: Internet
Author: User

Given an array of size n, find the majority element. The majority element is the element, the appears more than times ⌊ n/2 ⌋ .

Assume that the array was non-empty and the majority element always exist in the array.

Solution 1:

Compare naive's ideas, iterate through the array, use the map to save the number and the corresponding number. Then traverse keyset to read out the number greater than N/2.

Map is useful for handling the number of repetitions, and for recording the same key with different value changes.

Attention

int Change=res.get (nums[i]) +1;
Res.put (Nums[i],change);

If the direct res.put (Nums[i],res.get (nums[i]) + +) error occurs. I do not know the specific reason, the individual think is unable to read value and also change the value.

 Public classSolution { Public intMajorityelement (int[] nums) {Map<Integer,Integer> res=NewHashmap<integer,integer>(); intMajority=0;  for(inti=0;i<nums.length;i++)        {            if(!Res.containskey (Nums[i])) {Res.put (nums[i],1); }            Else            {                intChange=res.get (Nums[i]) +1;            Res.put (Nums[i],change); }        }         for(intNum:res.keySet ()) {            if(Res.get (num) > (NUMS.LENGTH/2) ) {Majority=num; }        }        returnmajority; }}

See the solution of discussion, feel oneself too naive = =

Solution 2:

 Public intMajorityElement2 (int[] nums) {Map<integer, integer> MyMap =NewHashmap<integer, integer>(); //Hashtable<integer, integer> myMap = new Hashtable<integer, integer> ();    intRet=0;  for(intnum:nums) {        if(!Mymap.containskey (num)) mymap.put (num,1); Elsemymap.put (num, mymap.get (num)+1); if(Mymap.get (num) >nums.length/2) {ret=num;  Break; }    }    returnret;}

No need to use iterator to check the keyset, the number of records and the number of times can be compared with the size of N/2.

Solution3:

 Public int majorityElement1 (int[] nums) {    arrays.sort (nums);     return NUMS[NUMS.LENGTH/2];}

This Nima is simply cheating!!

Solution4:

Boyer-moore Majority vote algorithm

Https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm

The core idea is to save a counter corresponding to canadiate, if there is really a choice times greater than half, then when the end of the cycle counter necessarily corresponding majority choice.

 public  int  MajorityElement3 (int  int  count=0, ret = 0< Span style= "color: #000000;"    >;  for  (int   Num:nums) { if  (Count==0 = num;  if  (Num!=ret) Count --;  else   count  ++;  return   ret;}  

Because the topic has already said certainly exists, therefore does not have to examine is more than half, exits the circulation counter certainly corresponds to the majority.

169. Majority Element

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.