LeetCode solutions

4068. Sum of Elements With Frequency Divisible by K

My accepted Python solution to LeetCode problem 4068, Sum of Elements With Frequency Divisible by K, running in 4ms.

  • Difficulty: Easy
  • Python
  • Runtime 4ms
  • Memory 17.3MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 4ms, memory 17.3MB, accepted 2025-12-30.

python
class Solution:
    def sumDivisibleByK(self, nums: List[int], k: int) -> int:
        from collections import Counter
        freq = Counter(nums)
        result = 0
        for num, count in freq.items():
            if count % k == 0:
                result += num * count
        return result

Source