Find sum of elements in an array with composite frequency in Java

Here, we will learn how to find the sum of numbers in an array with composite frequency in Java.

What is Composite Frequency?

Composite numbers are those numbers that have factors other than themselves and 1. Therefore, numbers that are not 1 and are not prime are known as composite numbers. Composite frequency means that the frequency of an element in an array is composite.

How to Find the Sum of Elements in an Array with Composite Frequency in Java?

Firstly, we will have to make a function to detect Prime numbers. In the following code snippet, we make this function where it returns True if it is a Prime number or if it is 0 or 1, otherwise False.

static boolean isPrime (int n) {
  if (n<=3)
    return true;
  if(n%2 ==0 || n%3 == 0)
    return false;
  for (int i =5; i*i<=n; i++) {
    if (n%i == 0 || n % (i+2) == 0)
      return false;
  }
  return true;
}

Next, we declare a HashMap to make a key-value pair. It will be used to store the frequency of the individual elements in the array. We will store the frequencies of the elements with the help of this function.

HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
  for (int k =0; k<n; k++) {
    if (!map.containsKey(arr[k]))
      map.put(arr[k], 1);
    else
      map.put(arr[k], map.get(arr[k])+1);
  }

 

After this, we will check whether the Values of the Keys are composite using the isPrime function. If they are composite, we will add them up in a ‘res’ variable.

for (Map.Entry<Integer,Integer> e: map.entrySet()) {
  if (!isPrime(e.getValue()))
    res += e.getKey()*e.getValue();
}
return res;

Finally, we write the driver program and call these upper functions to get the required result. Thus, this is how we find the sum of elements in an array with composite frequency in Java.

The Java Code and the Output

I have provided the entire code below for better understanding. Therefore, be sure to check it out.

import java.util.*;

public class compsum {
  
  static boolean isPrime (int n) {
    if (n<=3)
      return true;
    if(n%2 ==0 || n%3 == 0)
      return false;
    for (int i =5; i*i<=n; i++) {
      if (n%i == 0 || n % (i+2) == 0)
        return false;
    }
    return true;
  }
  
  static int getComSum (int arr[], int n) {
    int res =0;
    HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
    for (int i =0; i<n; i++) {
      if (!map.containsKey(arr[i]))
        map.put(arr[i], 1);
      else
        map.put(arr[i], map.get(arr[i])+1);
    }
    for (Map.Entry<Integer,Integer> e: map.entrySet()) {
      if (!isPrime(e.getValue()))
        res += e.getKey()*e.getValue();
    }
    return res;
  }
  
  public static void main (String args [] ) {
    int arr[] = {2,2,2,4,4,5,1,1,1,1,10,6,6,6,6,12,12,1,1,1,1,1};
    System.out.println(getComSum(arr, arr.length));
  }
}

The output will be like this:

33

I hope this post was helpful. Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *