Java Program to Find the Number Occurring Odd Number of Times


To find the number occurring odd number of times, the Java code is as follows −

Example

 Live Demo

public class Demo {
   static int odd_occurs(int my_arr[], int arr_size){
      int i;
      for (i = 0; i < arr_size; i++){
         int count = 0;
         for (int j = 0; j < arr_size; j++){
            if (my_arr[i] == my_arr[j])
               count++;
         }
         if (count % 2 != 0)
         return my_arr[i];
      }
      return -1;
   }
   public static void main(String[] args){
      int my_arr[] = new int[]{ 34, 56, 99, 34, 55, 99, 90, 11, 12, 11, 11, 34 };
      int arr_size = my_arr.length;
      System.out.println("The number that occurs odd number of times in the array is ");
      System.out.println(odd_occurs(my_arr, arr_size));
   }
}

Output

The number that occurs odd number of times in the array is
34

A class named Demo contains a static function named ‘odd_occurs’. This function iterates through the integer array, and checks to see the number of times these numbers occur. The odd number that occurs frequently is returned as output. In the main function, an integer array is defined, and the length of the array is assigned to a variable. The function is called by passing the array, and its length as parameters. Relevant message is displayed on the console.

Updated on: 08-Jul-2020

300 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements