// https://leetcode.com/problems/majority-element/
// Solved with Boyer–Moore majority vote algorithm
// https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm
public class Solution {
public int MajorityElement(int[] nums) {
int majority = nums[0];
int count = 0;
for (int i = 0; i < nums.Length; i++) {
if (count == 0)
majority = nums[i];
if (majority == nums[i])
count++;
else count--;
}
return majority;
}
}
-
MY PROJECTS
-
Recent Posts
- Algo.Java.DFS.Theory
- Java.Algo.DFSExample
- Algo.Java.Xor to find difference between strings
- Java.Algo.AllSubArrays
- Linux. Ubuntu.Monitor resources
- Java.Algo.Get sum of Digits from int
-
Java. List
to int[] and backwards - Sql.Execution Order
- Algo.Java.BinarySearch
- SQL. Conditional expression with count()
- Java.Algo.CumulativeApproach
- Algo.Java.BFS in BinaryTree
- Java.Jackson.Serialize object to json
- Algo. Traverse linked list
- Algo. Java. Remove-duplicates-from-sorted-array
- Java.Hibernate.SimpleExample
- Java.Jdbc.SimpleExample
- SQL. Calc the sum with case, example
- Mullvad – free the internet :)
- Junit. Jupiter
Categories
- Aptana
- Azure
- C#
- DataSnap
- DBExpress
- Delphi
- Delphi и сети
- Delphi. Язык программирования
- ExtJS
- FastReport
- FireDAC
- FireMonkey
- GIT
- ICS
- IDE
- IIS
- Indy
- InnoSetup
- javascript
- jQuery
- JSON
- LiveBindings
- MSHTML
- MySQL
- PHP
- REST
- Ribbons
- SMS
- SQL инструкции
- SVN
- TRichView
- UniGui
- WebBroker
- WinAPI
- Windows
- Алгоритмы
- Без рубрики
- Деревья
- Ищу ответ
- Компонентостроение
- Мои компоненты
- Начальный уровень
- Обработка исключений
- Парсинг
- Потоки(Threads)
- Регулярные выражения
- Тестирование приложений