Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
You may assume that the array is non-empty and the majority element always exist in the array.
(요약) 요소중 개수가 반 이상인 숫자를 return
하라.
var majorityElement = function(nums) { return nums.sort((a, b) => a - b)[nums.length / 2|0]; };
전에 코트카타 알고리즘 같이 공부하던 김동호님 코드가 생각나서 써봄.
정렬을 했을 때 반이상 이라면 무조건 배열의 중앙에 그 숫자가 위치해야 된다는 로직임.