返回介绍

solution / 2400-2499 / 2404.Most Frequent Even Element / README_EN

发布于 2024-06-17 01:03:06 字数 4915 浏览 0 评论 0 收藏 0

2404. Most Frequent Even Element

中文文档

Description

Given an integer array nums, return _the most frequent even element_.

If there is a tie, return the smallest one. If there is no such element, return -1.

 

Example 1:

Input: nums = [0,1,2,2,4,4,1]
Output: 2
Explanation:
The even elements are 0, 2, and 4. Of these, 2 and 4 appear the most.
We return the smallest one, which is 2.

Example 2:

Input: nums = [4,4,4,9,2,4]
Output: 4
Explanation: 4 is the even element appears the most.

Example 3:

Input: nums = [29,47,21,41,13,37,25,7]
Output: -1
Explanation: There is no even element.

 

Constraints:

  • 1 <= nums.length <= 2000
  • 0 <= nums[i] <= 105

Solutions

Solution 1: Hash Table

We use a hash table $cnt$ to count the occurrence of all even elements, and then find the even element with the highest occurrence and the smallest value.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array.

class Solution:
  def mostFrequentEven(self, nums: List[int]) -> int:
    cnt = Counter(x for x in nums if x % 2 == 0)
    ans, mx = -1, 0
    for x, v in cnt.items():
      if v > mx or (v == mx and ans > x):
        ans, mx = x, v
    return ans
class Solution {
  public int mostFrequentEven(int[] nums) {
    Map<Integer, Integer> cnt = new HashMap<>();
    for (int x : nums) {
      if (x % 2 == 0) {
        cnt.merge(x, 1, Integer::sum);
      }
    }
    int ans = -1, mx = 0;
    for (var e : cnt.entrySet()) {
      int x = e.getKey(), v = e.getValue();
      if (mx < v || (mx == v && ans > x)) {
        ans = x;
        mx = v;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int mostFrequentEven(vector<int>& nums) {
    unordered_map<int, int> cnt;
    for (int x : nums) {
      if (x % 2 == 0) {
        ++cnt[x];
      }
    }
    int ans = -1, mx = 0;
    for (auto& [x, v] : cnt) {
      if (mx < v || (mx == v && ans > x)) {
        ans = x;
        mx = v;
      }
    }
    return ans;
  }
};
func mostFrequentEven(nums []int) int {
  cnt := map[int]int{}
  for _, x := range nums {
    if x%2 == 0 {
      cnt[x]++
    }
  }
  ans, mx := -1, 0
  for x, v := range cnt {
    if mx < v || (mx == v && x < ans) {
      ans, mx = x, v
    }
  }
  return ans
}
function mostFrequentEven(nums: number[]): number {
  const cnt: Map<number, number> = new Map();
  for (const x of nums) {
    if (x % 2 === 0) {
      cnt.set(x, (cnt.get(x) ?? 0) + 1);
    }
  }
  let ans = -1;
  let mx = 0;
  for (const [x, v] of cnt) {
    if (mx < v || (mx === v && ans > x)) {
      ans = x;
      mx = v;
    }
  }
  return ans;
}
use std::collections::HashMap;
impl Solution {
  pub fn most_frequent_even(nums: Vec<i32>) -> i32 {
    let mut cnt = HashMap::new();
    for &x in nums.iter() {
      if x % 2 == 0 {
        *cnt.entry(x).or_insert(0) += 1;
      }
    }
    let mut ans = -1;
    let mut mx = 0;
    for (&x, &v) in cnt.iter() {
      if mx < v || (mx == v && ans > x) {
        ans = x;
        mx = v;
      }
    }
    ans
  }
}
class Solution {
  /**
   * @param Integer[] $nums
   * @return Integer
   */
  function mostFrequentEven($nums) {
    $max = $rs = -1;
    for ($i = 0; $i < count($nums); $i++) {
      if ($nums[$i] % 2 == 0) {
        $hashtable[$nums[$i]] += 1;
        if (
          $hashtable[$nums[$i]] > $max ||
          ($hashtable[$nums[$i]] == $max && $rs > $nums[$i])
        ) {
          $max = $hashtable[$nums[$i]];
          $rs = $nums[$i];
        }
      }
    }
    return $rs;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文