-
[알고리즘] #Majority Element■ Algorithm 2019. 7. 18. 17:11
출처 : https://leetcode.com/problems/majority-element/
Majority Element - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
문제
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.
풀이
array의 숫자들을 key로 하고, 나온 빈도수를 value로 하는 Map을 만들었다. 그 뒤, Map을 순회하면서 value값이 array의 길이의 절반보다 큰 key 값을 리턴하도록 했다.
코드
'■ Algorithm' 카테고리의 다른 글
[알고리즘] #First Bad Version (0) 2022.08.01 [알고리즘] #Binary Search (0) 2022.07.31 [알고리즘] #Merge Two Sorted Lists (0) 2019.07.12 [알고리즘] #Move Zeroes (0) 2019.07.08 [알고리즘] #Reverse Linked List (0) 2019.07.07