LeetCode 코딩 문제 2020/12/13 - Majority Element

이호현·2020년 12월 13일
0

Algorithm

목록 보기
30/138

[문제]

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];
};

전에 코트카타 알고리즘 같이 공부하던 김동호님 코드가 생각나서 써봄.
정렬을 했을 때 반이상 이라면 무조건 배열의 중앙에 그 숫자가 위치해야 된다는 로직임.

profile
평생 개발자로 살고싶습니다

0개의 댓글