// 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
- Java.Collections.What is the difference between java.util.Collection and java.util.Collections?
- Java.Collections.
- Java.Collections.Arrange the following interfaces in a hierarchy: List, Set, Map, SortedSet, SortedMap, Collection, Iterable, Iterator, NavigableSet, NavigableMap.
- Java.Collections.Why do i need insertion order and what is difference to sortedOrder ?
- Java.What is LinkedHashSet ?
- Java.Collections.What is SortedSet ?
- Java.Collections.Arrange the following interfaces in a hierarchy: List, Set, Map, SortedSet, SortedMap,
- Java.Collections.Name the main JCF interfaces and their implementations.
- Java.Collections.What is a “collection”?
- Java.Algo.1442. Count Triplets That Can Form Two Arrays of Equal XOR
- Java.Core.What is “internationalization”, “localization” in Java ?
- Java.Core.What are generics?
- Java.Core.Suppose there is a method that can throw IOException and FileNotFoundException, in what order should the catch blocks go? How many catch blocks will be executed?
- Java.Core.Can the main method throw an exception outside and if so, where will this exception be handled?
- Java.Core.Is the finally block always executed?
- Java.Core.Can one catch block catch several exceptions at once?
- Java.Core.Is it possible to use a try-finally block (without catch)?
- Java.Core.What is the try-with-resources mechanism?
- Java.Core.Describe the operation of the try-catch-finally block.
- Java.What do you know about OutOfMemoryError?
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)
- Регулярные выражения
- Тестирование приложений