Java Program to Find the Number Occurring Odd Number of Times
Last Updated :
09 Nov, 2023
Write a Python program for a given array of positive integers. All numbers occur an even number of times except one number which occurs an odd number of times. Find the number in O(n) time & constant space.
Examples :
Input: arr = {1, 2, 3, 2, 3, 1, 3}
Output : 3
Input: arr = {5, 7, 2, 7, 5, 2, 5}
Output: 5
Python Program to Find the Number Occurring Odd Number of Times using Nested Loop:
A Simple Solution is to run two nested loops. The outer loop picks all elements one by one and the inner loop counts the number of occurrences of the element picked by the outer loop.
Below is the implementation of the brute force approach :
Java
// Java program to find the element occurring
// odd number of times
class OddOccurrence {
// function to find the element occurring odd
// number of times
static int getOddOccurrence(int 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 (arr[i] == arr[j])
count++;
}
if (count % 2 != 0)
return arr[i];
}
return -1;
}
// driver code
public static void main(String[] args)
{
int arr[] = new int[]{ 2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2 };
int n = arr.length;
System.out.println(getOddOccurrence(arr, n));
}
}
// This code has been contributed by Kamal Rawal
Time Complexity: O(n^2)
Auxiliary Space: O(1)
Python Program to Find the Number Occurring Odd Number of Times using Hashing:
A Better Solution is to use Hashing. Use array elements as a key and their counts as values. Create an empty hash table. One by one traverse the given array elements and store counts.
Below is the implementation of the above approach:
Java
// Java program to find the element occurring odd
// number of times
import java.io.*;
import java.util.HashMap;
class OddOccurrence
{
// function to find the element occurring odd
// number of times
static int getOddOccurrence(int arr[], int n)
{
HashMap<Integer,Integer> hmap = new HashMap<>();
// Putting all elements into the HashMap
for(int i = 0; i < n; i++)
{
if(hmap.containsKey(arr[i]))
{
int val = hmap.get(arr[i]);
// If array element is already present then
// increase the count of that element.
hmap.put(arr[i], val + 1);
}
else
// if array element is not present then put
// element into the HashMap and initialize
// the count to one.
hmap.put(arr[i], 1);
}
// Checking for odd occurrence of each element present
// in the HashMap
for(Integer a:hmap.keySet())
{
if(hmap.get(a) % 2 != 0)
return a;
}
return -1;
}
// driver code
public static void main(String[] args)
{
int arr[] = new int[]{2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2};
int n = arr.length;
System.out.println(getOddOccurrence(arr, n));
}
}
// This code is contributed by Kamal Rawal
Time Complexity: O(n)
Auxiliary Space: O(n)
Python Program to Find the Number Occurring Odd Number of Times using Bit Manipulation:
The Best Solution is to do bitwise XOR of all the elements. XOR of all elements gives us odd occurring elements.
Here ^ is the XOR operators;
Note :
x^0 = x
x^y=y^x (Commutative property holds)
(x^y)^z = x^(y^z) (Distributive property holds)
x^x=0
Below is the implementation of the above approach.
Java
//Java program to find the element occurring odd number of times
class OddOccurrence
{
int getOddOccurrence(int ar[], int ar_size)
{
int i;
int res = 0;
for (i = 0; i < ar_size; i++)
{
res = res ^ ar[i];
}
return res;
}
public static void main(String[] args)
{
OddOccurrence occur = new OddOccurrence();
int ar[] = new int[]{2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2};
int n = ar.length;
System.out.println(occur.getOddOccurrence(ar, n));
}
}
// This code has been contributed by Mayank Jaiswal
Time Complexity: O(n)
Auxiliary Space: O(1)
Please refer complete article on Find the Number Occurring Odd Number of Times for more details!
Similar Reads
Java Program to Find sum of even factors of a number Given a number n, the task is to find the even factor sum of a number. Examples: Input : 30 Output : 48 Even dividers sum 2 + 6 + 10 + 30 = 48 Input : 18 Output : 26 Even dividers sum 2 + 6 + 18 = 26 Let p1, p2, ⦠pk be prime factors of n. Let a1, a2, .. ak be highest powers of p1, p2, .. pk respect
3 min read
Java program for Find sum of odd factors of a number Write a java program for a given number n, the task is to find the odd factor sum. Examples: Input: n = 30Output: 24Explanation: Odd dividers sum 1 + 3 + 5 + 15 = 24 Input: 18Output: 13Explanation: Odd dividers sum 1 + 3 + 9 = 13 Java program for Find sum of odd factors of a number using Prime facto
3 min read
Java Program for Number of elements with odd factors in given range Given a range [n, m], find the number of elements that have odd number of factors in the given range (n and m inclusive). Examples: Input : n = 5, m = 100 Output : 8 The numbers with odd factors are 9, 16, 25, 36, 49, 64, 81 and 100 Input : n = 8, m = 65 Output : 6 Input : n = 10, m = 23500 Output :
3 min read
Java Program to Find the Sum of First N Odd & Even Numbers When any number which ends with 0,2,4,6,8 is divided by 2 that is an even number. And when any number ends with 1,3,5,7,9 is not divided by two is an odd number. Example: Input : 8 Output: Sum of First 8 Even numbers = 72 Sum of First 8 Odd numbers = 64Approach #1: Iterative Create two variables eve
3 min read
Java Program to Find the Frequency of Odd & Even Numbers in the Matrix Matrix is simply a two-dimensional array. Just like in one-dimensional traversal operation is required to figure out. The task is to find the frequencies of odd and even number in the given matrix. The size of matrix is defined as product of numbers of columns and rows. So, the size of the matrix is
3 min read
Java Program to Find Maximum Odd Number in Array Using Stream and Filter Java 8 introduced some great features like Stream and Filter which tremendously simplify tasks like reading data and performing operations like minimum, maximum, sum, and conditional check on them. In this program, we will get the maximum of all odd numbers from a list of integers with the help of t
3 min read