29-numbers with more than half occurrences in an array

Source: Internet
Author: User

Title Description: http://ac.jobdu.com/problem.php?pid=1370
If there is a number in the array that appears more than half the length of the array, find this number. For example, enter an array of length 9 {1,2,3,2,2,2,5,4,2}. Since the number 2 appears in the array 5 times, which exceeds half the length of the array, the output is 2.

The intuitive idea is that first sort O (NLGN), then the subscript is N/2 position must be more than half the length of the array number.

Another way of thinking is:
using the partition function inside the fast line , randomly select an element, swap it smaller than it to the left, swap it large to the right, and then swap it to the middle. If this element is swapped to the position of the N/2, then the element is the median, and if the position of the element is less than N/2, then the median is on its right, the right side (index, n) to find the division, and the other on the left (0,index). time Complexity O (n), but modifies the contents of the original array.

The Third Way of thinking is:
A number appears more than half the length of the array, indicating that it appears more often than any other number in the array.
When we iterate through the array, we save 2 values, a number and a number of times.

    • If the next number is the same as the number saved, the number of times +1
    • If the next number differs from the number saved, the number of times-1
    • If the number is 0, save the next number

So the number you're looking for is the last number you set.
Time complexity O (n) without altering the contents of the array.

#include <iostream>using namespace STD;BOOLIsValid =true;intMorethanhalfnum (int* Nums,intLength) {if(nums = = NULL | | length <=0) {IsValid =false;return 0; }intresult = nums[0];intTimes =1; for(inti =1; i < length; i++) {if(Times = =0{result = Nums[i]; Times =1; }Else{if(Nums[i] = = result) times++;Elsetimes--; }    }returnResult;}intMain () {intA[] = {1,2,4,2,5,2,2,6,3,2,2};intresult = Morethanhalfnum (A,sizeof(a)/sizeof(a[0]));if(Result = =0&& IsValid = =false)cout<<"Input Error."<< Endl;Else        cout<< result << Endl;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

29-numbers with more than half occurrences in an array

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.