- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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.
Advertisements