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
One simple solution is to use two nested loops to count the occurrences of every element. While counting, we need to consider an element only if it is not already considered.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int sumKRepeating( int arr[], int n, int k)
{
int sum = 0;
vector< bool > visited(n, false );
for ( int i = 0; i < n; i++) {
if (visited[i] == true )
continue ;
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;
}
int main()
{
int arr[] = { 9, 9, 10, 11, 8, 8, 9, 8 };
int n = sizeof (arr)/ sizeof (arr[0]);
int k = 3;
cout << sumKRepeating(arr, n, k);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int sumKRepeating( int arr[],
int n, int k)
{
int sum = 0 ;
Vector<Boolean> visited = new Vector<Boolean>();
for ( int i = 0 ; i < n; i++)
visited.add( false );
for ( int i = 0 ; i < n; i++)
{
if (visited.get(i) == true )
continue ;
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;
}
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));
}
}
|
Python3
def sumKRepeating(arr, n, k):
sum = 0
visited = [ False for i in range (n)]
for i in range ( 0 , n, 1 ):
if (visited[i] = = True ):
continue
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
if __name__ = = '__main__' :
arr = [ 9 , 9 , 10 , 11 , 8 , 8 , 9 , 8 ]
n = len (arr)
k = 3
print (sumKRepeating(arr, n, k))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static int sumKRepeating( int [] arr,
int n, int k)
{
int sum = 0;
List< bool > visited = new List< bool >();
for ( int i = 0; i < n; i++)
{
visited.Add( false );
}
for ( int i = 0; i < n; i++)
{
if (visited[i] == true )
{
continue ;
}
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;
}
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));
}
}
|
Javascript
<script>
function sumKRepeating(arr,n,k)
{
let sum = 0;
let visited = [];
for (let i = 0; i < n; i++)
visited.push( false );
for (let i = 0; i < n; i++)
{
if (visited[i] == true )
continue ;
let count = 1;
for (let j = i + 1; j < n; j++)
{
if (arr[i] == arr[j])
{
count++;
visited[j]= true ;
}
}
if (count == k)
sum += arr[i];
}
return sum;
}
let arr=[9, 9, 10, 11,
8, 8, 9, 8 ];
let n = arr.length;
let k = 3;
document.write(sumKRepeating(arr, n, k));
</script>
|
Complexity Analysis:
- Time Complexity : O(n*n)
- Auxiliary Space : O(n)
An efficient solution is to use hashing. We count frequencies of all items. Then we traverse hash table and sum those items whose count of occurrences is k.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int sumKRepeating( int arr[], int n, int k)
{
int sum = 0;
unordered_map< int , int > mp;
for ( int i=0; i<n; i++)
mp[arr[i]]++;
for ( auto x : mp)
if (x.second == k)
sum += x.first;
return sum;
}
int main()
{
int arr[] = { 9, 9, 10, 11, 8, 8, 9, 8 };
int n = sizeof (arr)/ sizeof (arr[0]);
int k = 3;
cout << sumKRepeating(arr, n, k);
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map;
class GfG
{
static int sumKRepeating( int arr[], int n, int k)
{
int sum = 0 ;
HashMap<Integer, Integer> mp = new HashMap<>();
for ( int i= 0 ; i<n; i++)
{
if (!mp.containsKey(arr[i]))
mp.put(arr[i], 0 );
mp.put(arr[i], mp.get(arr[i]) + 1 );
}
for (Integer x : mp.keySet())
if (mp.get(x) == k)
sum += x;
return sum;
}
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));
}
}
|
Python3
import math as mt
def sumKRepeating(arr, n, k):
Sum = 0
mp = dict ()
for i in range (n):
if arr[i] in mp.keys():
mp[arr[i]] + = 1
else :
mp[arr[i]] = 1
for x in mp:
if (mp[x] = = k):
Sum + = x
return Sum
arr = [ 9 , 9 , 10 , 11 , 8 , 8 , 9 , 8 ]
n = len (arr)
k = 3
print (sumKRepeating(arr, n, k))
|
C#
using System;
using System.Collections.Generic;
class GfG
{
static int sumKRepeating( int []arr, int n, int k)
{
int sum = 0;
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);
}
}
foreach (KeyValuePair< int , int > entry in mp)
{
if (entry.Value >= k)
{
sum += entry.Key;
}
}
return sum;
}
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));
}
}
|
Javascript
<script>
function sumKRepeating(arr,n,k)
{
let sum = 0;
let mp = new Map();
for (let i=0; i<n; i++)
{
if (!mp.has(arr[i]))
mp.set(arr[i], 0);
mp.set(arr[i], mp.get(arr[i]) + 1);
}
for (let [key, value] of mp.entries())
if (mp.get(key) == k)
sum += key;
return sum;
}
let arr=[9, 9, 10, 11, 8, 8, 9, 8];
let n = arr.length;
let k = 3;
document.write(sumKRepeating(arr, n, k));
</script>
|
Complexity Analysis
- Time Complexity : O(n)
- Auxiliary Space : O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!