We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
原题链接:https://leetcode-cn.com/problems/majority-element/
解题思路:
/** * @param {number[]} nums * @return {number} */ var majorityElement = function (nums) { let map = new Map(); // 使用Map存储每个元素的的数量 // 遍历数组 for (const num of nums) { // 统计每个元素的数量 map.get(num) ? map.set(num, map.get(num) + 1) : map.set(num, 1); // 判断数量是否大于n/2,若是则直接返回 if (map.get(num) > nums.length / 2) { return num; } } };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
原题链接:https://leetcode-cn.com/problems/majority-element/
解题思路:
The text was updated successfully, but these errors were encountered: