# Sum of all elements repeating ‘k’ times in an array

Given an array we have to find the sum of all the elements repeating k times in an array. We need to consider every repeating element just once in the sum.

Examples:

```Input : arr[] = {2, 3, 9, 9}
k = 1
Output : 5
2 + 3 = 5

Input : arr[] = {9, 8, 8, 8, 10, 4}
k = 3
Output : 8
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

One simple solution is to use two nested loops to count occurrences of every element. While counting, we need to consider an element only if it is not already considered.

## C++

 `// C++ program find sum of elements that ` `// appear k times.  ` `#include ` `using` `namespace` `std; ` ` `  `// Function to count the sum ` `int` `sumKRepeating(``int` `arr[], ``int` `n, ``int` `k) ` `{ ` `    ``int` `sum = 0; ` ` `  `    ``// To keep track of processed elements ` `    ``vector<``bool``> visited(n, ``false``); ` ` `  `    ``// initializing count equal to zero ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// If arr[i] already processed ` `        ``if` `(visited[i] == ``true``) ` `            ``continue``; ` ` `  `        ``// counting occurrences of arr[i] ` `        ``int` `count = 1; ` `        ``for` `(``int` `j = i + 1; j < n; j++) {             ` `            ``if` `(arr[i] == arr[j]) { ` `                ``count++; ` `                ``visited[j] = ``true``; ` `            ``} ` `        ``} ` `   `  `        ``if` `(count == k) ` `           ``sum += arr[i]; ` `    ``} ` ` `  `    ``return` `sum; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 9, 9, 10, 11, 8, 8, 9, 8 }; ` `    ``int` `n = ``sizeof``(arr)/``sizeof``(arr); ` `    ``int` `k = 3; ` `    ``cout << sumKRepeating(arr, n, k); ` `    ``return` `0; ` `} `

## Java

 `// Java program find sum of  ` `// elements that appear k times.  ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` `// Function to count the sum ` `static` `int` `sumKRepeating(``int` `arr[],  ` `                         ``int` `n, ``int` `k) ` `{ ` `    ``int` `sum = ``0``; ` ` `  `    ``// To keep track of ` `    ``// processed elements ` `    ``Vector visited = ``new` `Vector(); ` `     `  `    ``for` `(``int` `i = ``0``; i < n; i++) ` `    ``visited.add(``false``); ` ` `  `    ``// initializing count ` `    ``// equal to zero ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `    ``{ ` ` `  `        ``// If arr[i] already processed ` `        ``if` `(visited.get(i) == ``true``) ` `            ``continue``; ` ` `  `        ``// counting occurrences of arr[i] ` `        ``int` `count = ``1``; ` `        ``for` `(``int` `j = i + ``1``; j < n; j++)  ` `        ``{          ` `            ``if` `(arr[i] == arr[j])  ` `            ``{ ` `                ``count++; ` `                ``visited.add(j, ``true``); ` `            ``} ` `        ``} ` ` `  `        ``if` `(count == k) ` `        ``sum += arr[i]; ` `    ``} ` ` `  `    ``return` `sum; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``int` `arr[] = { ``9``, ``9``, ``10``, ``11``, ` `                  ``8``, ``8``, ``9``, ``8` `}; ` `    ``int` `n = arr.length; ` `    ``int` `k = ``3``; ` `    ``System.out.println(sumKRepeating(arr, n, k)); ` `} ` `} ` ` `  `// This code is contributed by Arnab Kundu `

## Python3

 `# Python 3 program find sum of elements  ` `# that appear k times.  ` ` `  `# Function to count the sum ` `def` `sumKRepeating(arr, n, k): ` `    ``sum` `=` `0` ` `  `    ``# To keep track of processed elements ` `    ``visited ``=` `[``False` `for` `i ``in` `range``(n)] ` ` `  `    ``# initializing count equal to zero ` `    ``for` `i ``in` `range``(``0``, n, ``1``): ` `         `  `        ``# If arr[i] already processed ` `        ``if` `(visited[i] ``=``=` `True``): ` `            ``continue` ` `  `        ``# counting occurrences of arr[i] ` `        ``count ``=` `1` `        ``for` `j ``in` `range``(i ``+` `1``, n, ``1``): ` `            ``if` `(arr[i] ``=``=` `arr[j]): ` `                ``count ``+``=` `1` `                ``visited[j] ``=` `True` `     `  `        ``if` `(count ``=``=` `k): ` `            ``sum` `+``=` `arr[i] ` ` `  `    ``return` `sum` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``arr ``=` `[``9``, ``9``, ``10``, ``11``, ``8``, ``8``, ``9``, ``8``] ` `    ``n ``=` `len``(arr) ` `    ``k ``=` `3` `    ``print``(sumKRepeating(arr, n, k)) ` ` `  `# This code is contributed by ` `# Shashank_Sharma `

## C#

 `// c# program find sum of  ` `// elements that appear k times.  ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG ` `{ ` `// Function to count the sum  ` `public` `static` `int` `sumKRepeating(``int``[] arr,  ` `                                ``int` `n, ``int` `k) ` `{ ` `    ``int` `sum = 0; ` ` `  `    ``// To keep track of  ` `    ``// processed elements  ` `    ``List<``bool``> visited = ``new` `List<``bool``>(); ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``visited.Add(``false``); ` `    ``} ` ` `  `    ``// initializing count  ` `    ``// equal to zero  ` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` ` `  `        ``// If arr[i] already processed  ` `        ``if` `(visited[i] == ``true``) ` `        ``{ ` `            ``continue``; ` `        ``} ` ` `  `        ``// counting occurrences of arr[i]  ` `        ``int` `count = 1; ` `        ``for` `(``int` `j = i + 1; j < n; j++) ` `        ``{ ` `            ``if` `(arr[i] == arr[j]) ` `            ``{ ` `                ``count++; ` `                ``visited.Insert(j, ``true``); ` `            ``} ` `        ``} ` ` `  `        ``if` `(count == k) ` `        ``{ ` `            ``sum += arr[i]; ` `        ``} ` `    ``} ` ` `  `    ``return` `sum; ` `} ` ` `  `// Driver code  ` `public` `static` `void` `Main(``string``[] args) ` `{ ` `    ``int``[] arr = ``new` `int``[] {9, 9, 10, 11,  ` `                           ``8, 8, 9, 8}; ` `    ``int` `n = arr.Length; ` `    ``int` `k = 3; ` `    ``Console.WriteLine(sumKRepeating(arr, n, k)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by Shrikant13 `

Output:

```17
```

Time Complexity : O(n*n)
Auxiliary Space : O(n)

An efficient solution is use hashing. We count frequencies of all items. Then we traverse hash table and sum those items whose count of occurrences is k.

## C++

 `// C++ program find sum of elements that ` `// appear k times.  ` `#include ` `using` `namespace` `std; ` ` `  `// Returns sum of k appearing elements. ` `int` `sumKRepeating(``int` `arr[], ``int` `n, ``int` `k) ` `{ ` `   ``int` `sum = 0; ` ` `  `   ``// Count frequencies of all items ` `   ``unordered_map<``int``, ``int``> mp;  ` `   ``for` `(``int` `i=0; i

## Java

 `// Java program find sum of  ` `// elements that appear k times.  ` `import` `java.util.HashMap; ` `import` `java.util.Map; ` ` `  `class` `GfG ` `{ ` ` `  `    ``// Returns sum of k appearing elements.  ` `    ``static` `int` `sumKRepeating(``int` `arr[], ``int` `n, ``int` `k)  ` `    ``{  ` `        ``int` `sum = ``0``;  ` `         `  `        ``// Count frequencies of all items  ` `        ``HashMap mp = ``new` `HashMap<>();  ` `        ``for` `(``int` `i=``0``; i

## Python3

 `# Python3 program find Sum of elements ` `# that appear k times.  ` `import` `math as mt ` ` `  `# Returns Sum of k appearing elements. ` `def` `sumKRepeating(arr, n, k): ` `    ``Sum` `=` `0` ` `  `# Count frequencies of all items ` `    ``mp ``=` `dict``()  ` `    ``for` `i ``in` `range``(n): ` `        ``if` `arr[i] ``in` `mp.keys(): ` `            ``mp[arr[i]] ``+``=` `1` `        ``else``: ` `            ``mp[arr[i]] ``=` `1` `                 `  `# Sum items with frequencies equal to k. ` `    ``for` `x ``in` `mp: ` `        ``if` `(mp[x] ``=``=` `k): ` `            ``Sum` `+``=` `x ` `    ``return` `Sum` ` `  `# Driver code ` `arr ``=` `[ ``9``, ``9``, ``10``, ``11``, ``8``, ``8``, ``9``, ``8` `] ` `n ``=` `len``(arr) ` `k ``=` `3` `print``(sumKRepeating(arr, n, k)) ` ` `  `# This code is contributed  ` `# by Mohit kumar 29 `

## C#

 `// C# program find sum of  ` `// elements that appear k times. ` `using` `System; ` `using` `System.Collections.Generic; ` `class` `GfG  ` `{  ` ` `  `    ``// Returns sum of k appearing elements.  ` `    ``static` `int` `sumKRepeating(``int` `[]arr, ``int` `n, ``int` `k)  ` `    ``{  ` `        ``int` `sum = 0;  ` `         `  `        ``// Count frequencies of all items  ` `        ``Dictionary<``int``,``int``> mp = ``new` `Dictionary<``int``,``int``>(); ` `        ``for` `(``int` `i = 0 ; i < n; i++) ` `        ``{ ` `            ``if``(mp.ContainsKey(arr[i])) ` `            ``{ ` `                ``var` `val = mp[arr[i]]; ` `                ``mp.Remove(arr[i]); ` `                ``mp.Add(arr[i], val + 1);  ` `            ``} ` `            ``else` `            ``{ ` `                ``mp.Add(arr[i], 1); ` `            ``} ` `        ``} ` `        ``// Sum items with frequencies equal to k.  ` `        ``foreach``(KeyValuePair<``int``, ``int``> entry ``in` `mp) ` `        ``{ ` `            ``if``(entry.Value >= k) ` `            ``{ ` `                ``sum += entry.Key; ` `            ``} ` `        ``} ` `        ``return` `sum;  ` `    ``}  ` ` `  `    ``// Driver code  ` `    ``public` `static` `void` `Main(String []args)  ` `    ``{  ` `         `  `        ``int` `[]arr = { 9, 9, 10, 11, 8, 8, 9, 8 };  ` `        ``int` `n = arr.Length;  ` `        ``int` `k = 3;  ` ` `  `        ``Console.WriteLine(sumKRepeating(arr, n, k));  ` `    ``}  ` `}  ` ` `  `// This code contributed by Rajput-Ji `

Output:

```17
```

Time Complexity : O(n)
Auxiliary Space : O(n)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.