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; } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter