Find Majority Element in an Array
Problem:
Given an array of size n, find the element which occurs more than n/2 times. This element is called Majority Element.
For example:
Array 1: {2 6 2 2 6 2 2 8 2 1}
Majority Element: 2
Array 2: {1 7 8 2 6 8 1 3 2 8}
Majority Element: none
Algorithm 1:
Use 2 loops
In inner loop calculate the count of every element
In outer loop check, if count is greater than n/2, return the element.
If out of the loops, return null.
Time Complexity: O(n^2)
Algorithm2:
Sort the array
Again iterate over the array once and count occurrence of each element.
If an element is found with count is greater than n/2, return the element.
If no such element found, return null.
Time Complexity:
If O(nlogn) sorting algorithm is used, sorting step takes O(nlogn) time
Counting step takes O(n) time
Hence time complexity is O(nlogn)
Algorithm 3: Boyer-Moore Vote Algorithm
Step 1: Find a candidate for majority element.
Step 2: Check if this candidate is a majority element.
Step1:
Find the candidate for majority element
1: Initialize count of current candidate as 0, count = 0
2: Iterate over the array and do following steps:
(a) If count == 0, set candidate = array[i], count = 1
(b) Else
(i) If candidate == array[i], set count = count + 1
(ii) else set count = count - 1
Step 2:
Check if candidate is Majority Element
1: If count == 0, there is no majority element.
2: Else, iterate over array to get count of candidate.
(a) If count is greater than n/2, return candidate
(b) Else return null;
Code: http://www.ideserve.co.in/learn/find-majority-element-in-an-array
Website: http://www.ideserve.co.in
Facebook: https://www.facebook.com/IDeserve.co.in
Что делает видео по-настоящему запоминающимся? Наверное, та самая атмосфера, которая заставляет забыть о времени. Когда вы заходите на RUVIDEO, чтобы посмотреть онлайн «Find Majority Element in an Array», вы рассчитываете на нечто большее, чем просто загрузку плеера. И мы это понимаем. Контент такого уровня заслуживает того, чтобы его смотрели в HD 1080, без дрожания картинки и бесконечного буферизации.
Честно говоря, Rutube сегодня — это кладезь уникальных находок, которые часто теряются в общем шуме. Мы же вытаскиваем на поверхность самое интересное. Будь то динамичный экшн, глубокий разбор темы от любимого автора или просто уютное видео для настроения — всё это доступно здесь бесплатно и без лишних формальностей. Никаких «заполните анкету, чтобы продолжить». Только вы, ваш экран и качественный поток.
Если вас зацепило это видео, не забудьте взглянуть на похожие материалы в блоке справа. Мы откалибровали наши алгоритмы так, чтобы они подбирали контент не просто «по тегам», а по настроению и смыслу. Ведь в конечном итоге, онлайн-кинотеатр — это не склад файлов, а место, где каждый вечер можно найти свою историю. Приятного вам отдыха на RUVIDEO!
Видео взято из открытых источников Rutube. Если вы правообладатель, обратитесь к первоисточнику.