↓Skip to main content
  1. LeetCode/

LeetCode 3712: Sum of Elements With Frequency Divisible by K

·1 min· ·
LeetCode Easy Hash-Table Counting Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2025-10-12

  • Submissions: Accepted (first try)

Approach
#

Count each value with a Counter (Hash Table Template 1). For a value whose count is divisible by k, every occurrence counts, so add value × count.

My Solution
#

class Solution:
    def sumDivisibleByK(self, nums: List[int], k: int) -> int:
        return sum(x * c for x, c in Counter(nums).items() if c % k == 0)

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • Add every occurrence, not the value once.

Interview Explanation
#

I count each value, and when its count is divisible by k I add the value times its count. Time O(n).

Related

LeetCode 1160: Find Words That Can Be Formed by Characters
·1 min
LeetCode Easy Array Hash-Table String Counting
LeetCode 1160 note: a word is formable when none of its character counts exceeds chars’; sum the lengths of such words
LeetCode 1128: Number of Equivalent Domino Pairs
·2 mins
LeetCode Easy Array Hash-Table Counting
LeetCode 1128 note: normalize each domino to (small, large) as a key and count; a key with c dominoes yields c(c - 1) / 2 pairs
LeetCode 914: X of a Kind in a Deck of Cards
·2 mins
LeetCode Easy Array Hash-Table Math Counting
LeetCode 914 note: every card count must be divisible by one X >= 2, i.e. the GCD of all counts is at least 2
LeetCode 819: Most Common Word
·2 mins
LeetCode Easy Array Hash-Table String Counting
LeetCode 819 note: replace punctuation with spaces, lowercase, split, count, and pick the most frequent word not banned
LeetCode 3545: Minimum Deletions for At Most K Distinct Characters
·1 min
LeetCode Easy Greedy Hash-Table String Sorting Counting
LeetCode 3545 note: reducing the number of kinds means deleting whole kinds, starting from the least frequent
LeetCode 3541: Find Most Frequent Vowel and Consonant
·1 min
LeetCode Easy Hash-Table String Counting
LeetCode 3541 note: count characters, take the maximum among vowels and among consonants and add them; a missing class counts as 0