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
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 4ms, memory 17.3MB, accepted 2025-12-30.
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