Solution 2Sort the given array with best sorting set of rules say merge sort. Take the given array first position as your majority aspect . from the second one element of the given array compare with the old aspect and increment count of the element by 1 until the aspect adjustments. check if the count is above n/2 where n is given . If its above n/2 break the loop and return majority aspect.
if it is not above n/2 continue the loop till end of the array. At the last examine if the count is above n/2 and return majority aspect else return no majority element found. Code snippet for an analogous as followsint majorityint a,int n else } else}if count >= n/2return 1;}Time complexity – Onlogn . sorting and comparing all aspects in the array Space complexity – O1 – Additional space not required to store count for the features. just one count is maintainedSolution 3 Sort the elements in the array with best sorting algorithmTake majority element as n/2th aspect. evaluate to the left of the general public aspect and hold count till element is changed.
evaluate to the right of the aspect and hold same count till aspect is modified. At the end count if above n/2 return majority element or return no majority aspect. Time complexity – Onlogn . sorting and comparing all features in the array Space complexity – O1 – Additional space not required to store count for the facets. just one count is maintainedBut we dont are looking to traverse via entire array and number if assessment are minimal. Comparisons are cpu in depth.