3 arrays/majority element/main.cpp
//Based on Moore's voting algorithm O(n)
Based on Moore's voting algorithm O(n)
Based on Moore ' s voting algorithm O ( n )
int majority2(int arr[], int n){        
    int res=0, count=1;
    for(int i=1; i<n; i++){
        if(arr[i] == arr[i-1])
            count++;
        else
            count--;
        if(count == 0){
            res = i;
            count = 1;
        }
    }
    count = 0;
    for(int i=0; i<n; i++){
        if(arr[res] == arr[i])
            count++;
    }
    return res;
}
int majority2 ( int arr [ ] , int n ) { int res = 0 , count = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) count ++ ; else count -- ; if ( count == 0 ) { res = i ; count = 1 ; } } count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ res ] == arr [ i ] ) count ++ ; } return res ; }
Based on Moore's voting algorithm O(n)
Based on Moore ' s voting algorithm O ( n )
//Might not return index of first occurence of majority ele