Snippets Collections
class Solution {
    // Function to find element with more appearances between two elements in an
    // array.
    public int majorityWins(int arr[], int n, int x, int y) 
    {
        int countX=0;
        int countY=0;
        for(int i=0;i<n;i++)
        {
            if(arr[i]==x)
                countX++; 
             
             if(arr[i]==y)
                 countY++;
         }
         
        if(countX>countY)
            return x;
         
        else if(countX==countY && x<y)
            return x;
         
        else 
            return y;
    }
}
star

Tue Feb 08 2022 06:33:11 GMT+0000 (UTC) https://practice.geeksforgeeks.org/problems/who-has-the-majority/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #majority

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension