- Related Questions & Answers
- C/C++ Program to Find the Number Occurring Odd Number of Times?
- C/C++ Program for Finding the Number Occurring Odd Number of Times?
- Python program to find the number occurring odd number of times using Lambda expression and reduce function
- Find the Number Occurring Odd Number of Times using Lambda expression and reduce function in Python
- Golang Program to find the odd-occurring elements in a given array
- Finding number that appears for odd times - JavaScript
- Program to find most occurring number after k increments in python
- Java program to find whether given number is even or odd
- How to find the one integer that appears an odd number of times in a JavaScript array?
- C++ Program to calculate the number of odd days in given number of years
- C Program for Find sum of odd factors of a number?
- Python Program for Find sum of odd factors of a number
- C++ program for Find sum of odd factors of a number
- Take an array and find the one element that appears an odd number of times in JavaScript
- C++ program to concatenate a string given number of times?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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)); } }

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.

Advertisements