Count Repeated Elements in Array in Java

How to count repeated elements in an array in Java programming language. If the array is sorted then counting repeated elements in an array will be easy compare to the unsorted array.

Example1- an unsorted array,
Array = { 50, 20, 10, 40, 20, 10, 10, 60, 30, 70 };
Total Repeated elements: 2
Repeated elements are: 20 10

Example2- a sorted array,
Array = { 10, 10, 10, 20, 20, 30, 40, 50, 60, 70 };
Total Repeated elements: 2
Repeated elements are: 10 20

Java Program to Count Repeated Elements in an Array

The below program is applicable on any array which can be a sorted or an unsorted array. Here we will create a temporary array of similar length, traverse through the original array, and if the repeated element is found then insert it in the temporary array. If the next element is already available in the temporary array then skip it. The procedure to solve this problem,

a) Take an array
b) Create a new temporary array (assuming for the worst case when there are no duplicate elements)
c) Traverse through the original array
d) If the current element is available in the temporary array then skip checking for the current element.
e) Else compare the current element and all next elements.
f) If the match found then insert it into the temporary array, and stop comparing with the next elements.
g) Finally, display total repeated elements.

Java Program to count repeated elements in an array

public class ArrayTest {

   public static void main(String[] args) {

      // original array
      int arr[] = { 50, 20, 10, 40, 20, 10, 10, 60, 30, 70};
      
      // create another array of similar size
      int temp[] = new int[arr.length];
      int count = 0;
      
      // traverse original array
      for(int i=0; i<arr.length; i++) {
         
         int element = arr[i];
         boolean flag = false;
         
         // check current element is already 
         // checked or not
         for(int j=0; j<count; j++) {
            if(temp[j] == element) {
               flag = true;
               break;
            }
         }
         
         // if already exist then don't check
         if(flag) {
            continue;
         }
         
         // check occurrence of element
         for(int j=i+1; j<arr.length; j++) {
            if(arr[j] == element) {
               temp[count++] = element;
               // found, therefore break
               break;
            }
         }
      }
      
      // display total repeated elements
      System.out.println("Total Repeated elements: " + count);
      // display repeated elements
      System.out.println("Repeated elements are: ");
      for (int i = 0; i < count; i++) {
         System.out.print(temp[i]+" ");
      }      
   }
}

Output:-

Total Repeated elements: 2
Repeated elements are:
20 10

Program only for the Sorted array

The below program is applicable only for the sorted array in ascending order, not for the unsorted array or sorted in descending order. To apply on unsorted array first sort the given array in ascending order using Arrays.sort() method.

Java program to count repeated elements in sorted array in Java

public class ArrayTest {

   public static void main(String[] args) {

      // original array
      int arr[] = { 10, 10, 10, 20, 20, 30, 40, 50, 60, 70 };

      // create another array of similar size
      int temp[] = new int[arr.length];
      int count = 0;

      // traverse original array
      for (int i = 1; i < arr.length; i++) {
         
         // current element
         int element = arr[i];

         // if already exist then don't check
         if(element == temp[count]) {
            continue;
         } 
         
         // check occurrence of element
         for (int j = i + 1; j < arr.length; j++) {
            if (arr[j] == element) {
               temp[count++] = element;
               // found, therefore break
               break;
            }
         }
      }

      // display total repeated elements
      System.out.println("Total Repeated elements: " + count);

      // display repeated elements
      System.out.println("Repeated elements are: ");
      for (int i = 0; i < count; i++) {
         System.out.print(temp[i] + " ");
      }
   }
}

Output:-

Total Repeated elements: 2
Repeated elements are:
10 20

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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