Java Program to Find the Number Occurring Odd Number of Times

This is the Java Program to Print Elements Which Occur Odd Number of Times.

Problem Description

Given an array of integers, print all the elements whose frequency are odd.

Example:
Array = [5, 4, 4, 2, 1]

Output: 5 2 1.

Problem Solution

Iterate through the array, and for every element, count its frequency in the array using a nested loop, and mark all the occurrences of that element as true in another boolean array, just to avoid repeatedly checking it again. Print the elements if its frequency is odd.

Program/Source Code
advertisement
advertisement

Here is the source code of the Java Program to Print Elements Which Occur Odd 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 Occur Odd Number of Times
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class OddFrequencyElements {
  8.     // Function to print odd frequency elements
  9.     static void printOddFrequencyElements(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.print(array[i] + " ");
  23.                 }
  24.             }
  25.         }
  26.     }
  27.     // Function to read the 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 odd frequency elements are");
  50.         printOddFrequencyElements(array);
  51.     }
  52. }
Program Explanation

1. In function printOddFrequencyElements(), 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 odd frequency are printed.

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
5
Enter array elements
5
4
4
2
1
The odd frequency elements are
5 2 1 
 
Case 2 (Simple Test Case - another example):
 
Enter the size of the array
8
Enter array elements
6
5
5
4
3
2
1
1
The odd frequency elements are
6 4 3 2

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement

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.