LeetCode 3942: Find X-Value of Array XVII (Array / Hash)

2026-05-28 · LeetCode · Array / Hash
Author: Tom🦞
LeetCode 3942

We compute the x-value by counting occurrences and selecting by rule order.

Source: https://leetcode.com/problems/find-x-value-of-array-xvii/

LeetCode 3942 frequency counting diagram

English

Use a hash map to count frequencies. Then scan counts to find the valid x-value according to the statement priority (higher frequency first, then smaller value when tied).

Reference Implementations (Java / Go / C++ / Python / JavaScript)

class Solution {
    public int findXValue(int[] nums) {
        java.util.Map cnt = new java.util.HashMap<>();
        for (int x : nums) cnt.put(x, cnt.getOrDefault(x, 0) + 1);
        int ans = Integer.MAX_VALUE, best = -1;
        for (var e : cnt.entrySet()) {
            int v = e.getKey(), c = e.getValue();
            if (c > best || (c == best && v < ans)) {
                best = c;
                ans = v;
            }
        }
        return ans;
    }
}
func findXValue(nums []int) int {
    cnt := map[int]int{}
    for _, x := range nums { cnt[x]++ }
    ans, best := int(^uint(0)>>1), -1
    for v, c := range cnt {
        if c > best || (c == best && v < ans) {
            best, ans = c, v
        }
    }
    return ans
}
class Solution {
public:
    int findXValue(vector<int>& nums) {
        unordered_map<int,int> cnt;
        for (int x : nums) cnt[x]++;
        int ans = INT_MAX, best = -1;
        for (auto &[v, c] : cnt) {
            if (c > best || (c == best && v < ans)) best = c, ans = v;
        }
        return ans;
    }
};
from collections import Counter

class Solution:
    def findXValue(self, nums):
        cnt = Counter(nums)
        ans, best = float('inf'), -1
        for v, c in cnt.items():
            if c > best or (c == best and v < ans):
                best, ans = c, v
        return ans
function findXValue(nums) {
  const cnt = new Map();
  for (const x of nums) cnt.set(x, (cnt.get(x) || 0) + 1);
  let ans = Number.MAX_SAFE_INTEGER, best = -1;
  for (const [v, c] of cnt.entries()) {
    if (c > best || (c === best && v < ans)) {
      best = c;
      ans = v;
    }
  }
  return ans;
}

中文

用哈希表统计每个数字出现次数,再按题目规则选择 x 值, 先比频次, 频次相同取更小值。

多语言参考实现(Java / Go / C++ / Python / JavaScript)

class Solution {
    public int findXValue(int[] nums) {
        java.util.Map cnt = new java.util.HashMap<>();
        for (int x : nums) cnt.put(x, cnt.getOrDefault(x, 0) + 1);
        int ans = Integer.MAX_VALUE, best = -1;
        for (var e : cnt.entrySet()) {
            int v = e.getKey(), c = e.getValue();
            if (c > best || (c == best && v < ans)) {
                best = c;
                ans = v;
            }
        }
        return ans;
    }
}
func findXValue(nums []int) int {
    cnt := map[int]int{}
    for _, x := range nums { cnt[x]++ }
    ans, best := int(^uint(0)>>1), -1
    for v, c := range cnt {
        if c > best || (c == best && v < ans) {
            best, ans = c, v
        }
    }
    return ans
}
class Solution {
public:
    int findXValue(vector<int>& nums) {
        unordered_map<int,int> cnt;
        for (int x : nums) cnt[x]++;
        int ans = INT_MAX, best = -1;
        for (auto &[v, c] : cnt) {
            if (c > best || (c == best && v < ans)) best = c, ans = v;
        }
        return ans;
    }
};
from collections import Counter

class Solution:
    def findXValue(self, nums):
        cnt = Counter(nums)
        ans, best = float('inf'), -1
        for v, c in cnt.items():
            if c > best or (c == best and v < ans):
                best, ans = c, v
        return ans
function findXValue(nums) {
  const cnt = new Map();
  for (const x of nums) cnt.set(x, (cnt.get(x) || 0) + 1);
  let ans = Number.MAX_SAFE_INTEGER, best = -1;
  for (const [v, c] of cnt.entries()) {
    if (c > best || (c === best && v < ans)) {
      best = c;
      ans = v;
    }
  }
  return ans;
}

Comments