Java Program to Find the Number Occurring Even Number of Times

This is the Java Program to Print Elements Which Occurs Even Number of Times.

Problem Description

Given an array of elements, print the elements whose frequency is even.
Example:

array = {5, 5, 2, 2, 2, 4, 4, 1, 7, 1}
Output = 5 4 1

Problem Solution

Create a separate array of boolean values of the same length as that of the original array, and initialize it to false. Iterate through the array to find the frequency of the frequency of each element and also mark the index which is already visited as true, to avoid printing the same element twice. Print the elements with even frequency.

Program/Source Code

Here is the source code of the Java Program to Print Elements Which Occurs Even Number of Times. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

  1.  
  2. //Java Program to Print Elements Which Occurs Even Number of Times
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class EvenFrequencyElements {
  8.     // Function to print even frequency elements
  9.     static void printEvenFrequencyElements(int[] array){
  10.         boolean[] check = new boolean[array.length];
  11.         int i,j,count;
  12.         for(i=0; i<array.length; i++){
  13.             if(!check[i]){
  14.                 count=1;
  15.                 for(j=i+1;j<array.length;j++){
  16.                     if(array[j] == array[i]){
  17.                         count++;
  18.                         check[j]=true;
  19.                     }
  20.                 }
  21.                 if(count%2==0){
  22.                     System.out.println(array[i]);
  23.                 }
  24.             }
  25.         }
  26.     }
  27.     // Main function to read input
  28.     public static void main(String[] args) {
  29.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  30.         int size;
  31.         System.out.println("Enter the size of the array");
  32.         try {
  33.             size = Integer.parseInt(br.readLine());
  34.         } catch (Exception e) {
  35.             System.out.println("Invalid Input");
  36.             return;
  37.         }
  38.         int[] array = new int[size];
  39.         System.out.println("Enter array elements");
  40.         int i;
  41.         for (i = 0; i < array.length; i++) {
  42.             try {
  43.                 array[i] = Integer.parseInt(br.readLine());
  44.             } catch (Exception e) {
  45.                 System.out.println("An error occurred");
  46.                 return;
  47.             }
  48.         }
  49.         System.out.println("The even frequency elements are");
  50.         printEvenFrequencyElements(array);
  51.     }
  52. }
Program Explanation

1. In function printEvenFrequencyElements(), a boolean array check is created to store the status if the array element is already visited or not.
2. The loop for(i=0; i<array.length; i++) iterates through the array.
3. The condition if(!check[i]) checks if the element is already visited.
4. If the element is not visited already, then the count is initialized to 1 and a nested loop for(j=i+1;j<array.length;j++).
5. The elements with even frequency are printed.

advertisement
advertisement

Time Complexity: O(n2) where n is the number of elements in the array.

Runtime Test Cases
 
Case 1 (Simple Test Case):
 
Enter the size of the array
10
Enter array elements
5
5
2
2
2
4
4
1
7
1
The even frequency elements are
5 4 1
 
Case 2 (Simple Test Case - another example):
 
Enter the size of the array
9
Enter array elements
1
2
1
2
3
3
3
8
9
The even frequency elements are
1 2

Sanfoundry Global Education & Learning Series – Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.