Find Count of Positive, Negative and Zero Elements in an Array in Java


In Java, Array is a non-primitive data type which stores values of similar data type.

As per the problem statement we have to find the frequency of each element i.e how many times each element is occurring in an array.

Let’s see how we can do it by using the Java programming language.

To Show You Some Instances

Instance-1

Suppose the original array is {21, 10, 26, 21, 10, 33, 33, 20, 10, 21}

After finding the frequency of each element of an array the result will be −

Element | Frequency
------------------------
21 | 3
10 | 3
26 | 1
33 | 2
20 | 1

Instance-2

Suppose the original array is {2, 3 ,2, 3, 9, 5, 3, 2, 9, 1}

After finding the frequency of each element of an array the result will be −

Element | Frequency
-----------------------
2 | 3
3 | 3
9 | 2
5 | 1
1 | 1

Algorithm

  • Step 1 − Declare and initialize an integer array.

  • Step 2 − Traverse through array elements and count frequencies using for loop.

  • Step 3 − Skip the element if already processed and Count frequency using another for loop.

  • Step 4 − Print the desired result.

Syntax

To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.

Below refers to the syntax of it −

array.length

where, ‘array’ refers to the array reference.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Initialization of Array Elements

  • By Using User Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using Static Initialization of Array Elements

Example

In this approach, array elements will be initialized in the program. Then as per the algorithm traverse through array elements and count frequencies using for loop and Count frequency using another for loop.

import java.util.Arrays;
public class Main{

   //main method
   public static void main(String args[]){
   
      //Declare and initialize the array elements
      int arr[] = new int[]{ 21, 10, 26, 21, 10, 33, 33, 20, 10, 21 };
      int n = arr.length;
      boolean vis[] = new boolean[n];
      Arrays.fill(vis, false);

      System.out.println("---------------------------------------");  
      System.out.println(" Element | Frequency");  
      System.out.println("---------------------------------------");
      // Traverse through array elements and count frequencies
      for (int i = 0; i < n; i++){
      
         // Skip this element if already processed
         if (vis[i] == true)
            continue;

         //to Count frequency
         int count = 1;
         for (int j = i + 1; j < n; j++){
            if (arr[i] == arr[j]){
               vis[j] = true;
               count++;
            }
         }
         System.out.println(arr[i] + " | " + count);
      }
      System.out.println("----------------------------------------");  
   }
}

Output

---------------------------------------
 Element | Frequency
---------------------------------------
21 | 3
10 | 3
26 | 1
33 | 2
20 | 1
----------------------------------------

Approach-2: By Using User Defined Method

Example

In this approach, array elements will be initialized in the program. Then call a user defined method by passing the array as parameter and inside method as per the algorithm traverse through array elements and count frequencies using for loop and Count frequency using another for loop.

import java.util.Arrays;
public class Main{

   //user defined method
   public static void frequency(int arr[], int n){
      boolean vis[] = new boolean[n];
      Arrays.fill(vis, false);

      System.out.println("---------------------------------------");  
      System.out.println(" Element | Frequency");  
      System.out.println("---------------------------------------");
      // Traverse through array elements and count frequencies
      for (int i = 0; i < n; i++) {

         // Skip this element if already processed
         if (vis[i] == true)
            continue;

         //to Count frequency
         int count = 1;
         for (int j = i + 1; j < n; j++) {
            if (arr[i] == arr[j]) {
               vis[j] = true;
               count++;
            }
         }
         System.out.println(arr[i] + " | " + count);
      }
      System.out.println("----------------------------------------");  
   }


   //main method
   public static void main(String args[]){
   
      //Declare and initialize the array elements
      int arr[] = new int[]{ 88, 22, 88, 22, 15, 32, 22, 32, 89};
      int number = arr.length;
      
      //call a user defined method
      frequency(arr, number);
   }
}

Output

---------------------------------------
 Element | Frequency
---------------------------------------
88 | 2
22 | 3
15 | 1
32 | 2
89 | 1
----------------------------------------

In this article, we explored how to find the frequency of each element of an array in Java.

Updated on: 05-Jan-2023

310 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements