Find the element that appears more than half of the number of times in the array

Source: Internet
Author: User

Question:Find the element that appears more than half of the number of times in the array

Solution:Two different elements in the array are deleted each time. After deletion, the number of elements to be searched still exceeds half of the total number of deleted elements.

#include <stdio.h>int half_number(int a[], int n){    if( a == NULL || n <= 0 )        return -1;    int i, candidate;    int times = 0;    for( i=0; i<n; i++ )    {        if( times == 0 )        {            candidate = a[i];            times = 1;        }        else if( a[i] == candidate )            ++times;        else            --times;    }    return candidate;}int main(void){    int a[] = {1,2,3,2,2,2,5,4,2};    int result = half_number(a, 9);    if( result != -1 )        printf("%d\n", result);    else        printf("Error.\n");    return 0;}

 

Extension of this question:The number of times three elements appear in the array exceeds 1/4 of the total number of elements n.

Solution:Same as above, but each time you delete four elements that are different from each other, it is slightly more difficult to process

# Include <stdio. h> void find (int A [], int N) {if (a = NULL | n <= 3) {printf ("error. \ n "); return;} int I, j; int Times [3] = {0, 0 }; // count the three candidates int candidate [3] = {-1,-1,-1}; // assume that the element cannot be-1 for (I = 0; I <n; I ++) {If (Times [0] = 0 & A [I]! = Candidate [1] & A [I]! = Candidate [2]) // When 1st candidates are currently vacant and the current element a [I] is not equal to the other two candidates, use this element as the new candidate {candidate [0] = A [I]; Times [0] = 1 ;} if (Times [1] = 0 & A [I]! = Candidate [0] & A [I]! = Candidate [2]) {candidate [1] = A [I]; Times [1] = 1 ;} if (Times [2] = 0 & A [I]! = Candidate [1] & A [I]! = Candidate [0]) {candidate [2] = A [I]; Times [2] = 1;} else if (a [I] = candidate [0]) {+ Times [0];} else if (a [I] = candidate [1]) {++ Times [1];} else if (a [I] = candidate [2]) {++ Times [2];} else // deletes four different array elements, after deletion, {-- Times [0]; -- Times [1]; -- Times [2] ;}} printf ("% d \ n ", candidate [0], candidate [1], candidate [2]);} int main (void) {int A [] = {5, 1,, 2}; find (A, 23); Return 0 ;}

 

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.