Algorithm Learning (11) The number of occurrences in an array that exceeds half the length

Source: Internet
Author: User

number of occurrences in an array that exceeds half the length of the array

Title Description:
Given an array, find the number of occurrences in the array that are more than half the length of the array, such as a[]= {0,1,2,1,1}, with an output of 1
Analysis:
1, you can use the accompanying array B, iterate over array A, the value as the subscript of array B, count the number of times as the value, then iterate over the array B, find more than half the value, and then output. The time complexity is O (N), but increase the complexity of the space.
2, we can sort the array first, because a number appears more than half, so at the n/2 of the arrays, it must be that number. The time complexity is mainly for the sort of time, using the Fast row O (N*logn).
3, there is a number of times than all the other array occurrences of the sum of more, then we can walk the array, save two values, one for the number, one for the number of times, if the next number and the number of saved, the number is reduced by one, if and save the same, the number is added one, if the number is zero, Save the next number and reset the number to 1 until the end of the array, and the saved number is needed. The number of times used is the sum of the number of the other than the sum of the other numbers, the difference is reduced, the final number is certainly not 0.
Example: 0,1,2,1,1:
i = 0,result = 0,times = 1;
i = 1,a[1]! = result,times–;times= 0;
i = 2, because times = 0;result = A[2],times resets to 1;
I= 3,a[3]! = result;times–;times = 0;
i = 4, result = 1;times = 1;
Output result = 1;

#include <iostream>using namespace Std;intFind (int* Data,int length){intResult Times; Times=0;intI for(i =0;i<length; i++) {if( Times==0{result = Data[i]; Times=1; }Else{if(Result = = Data[i]) Times++;Else                 Times--; }    }returnResult;}intMain () {inta[5] = {0,1,2,1,1}; cout << Find (A,5) << Endl;return 0;}
When the number in the array is half the length

This time, the above method is not, because when traversing to the last number, times = 0, this time we need the last number, but the array has been traversed, the save may be the previous number.
such as 0,1,2,1
i = 0, result =0,times = 1;
i = 1,a[1]!= result;times–;times = 0;
i = 2, result = 2;times = 1;
i = 3,a[3]! = result,times–;times = 0;
Output result = 2, but we need to return 2 for the next number 1.
If it's 0,2,1,1,
i = 0;result = 0;times= 1;
i = 1,a[1]! = result; times–;times= 0;
i = 2,result = 1,times = 1;
i = 3,a[3] = = Result;times++;times = 2;
Output result = 1;
We will find that, in fact, we have to judge the comparison between the saved result and the last element at the end of the loop, and the final output is selected from two numbers.
We can declare a variable ntimes for the last element, iterate over the array, judge if the current element is equal to the last element, ntimes the number of times plus one, and the last direct judgment, ntimes = = N/2, if True, indicates that the last element is, if False, Indicates that the number saved in result is the one you are looking for.

intFind2 (int* Data,int length){intResult Times, Ntimes; Times= Ntimes =0;intI for(i =0;i<length; i++) {if(Data[i] = = data[length-1]) ntimes++;//Number of occurrences of the last elementif( Times==0{result = Data[i]; Times=1; }Else{if(Data[i] = = result) Times++;Else                 Times--; }    }if(Ntimes * *==length)returndata[length-1];Else        returnResult;}

Algorithm Learning (11) The number of occurrences in an array that exceeds half the length

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.