Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Returning only odd number from array in JavaScript
JavaScript arrays often contain mixed data types, and finding specific patterns like the one odd number among evens (or vice versa) is a common programming challenge. This article demonstrates how to identify and return the single different element from an array.
Problem Statement
We need to write a JavaScript function that takes an array of integers as input. The array contains either all even numbers with one odd number, or all odd numbers with one even number. Our function should return this single different element.
For example, if the input array is:
const arr = [5, 9, 7, 11, 34, 23, 77];
The expected output is 34 because it's the only even number among odd numbers.
Solution Using Separate Arrays
The first approach separates even and odd numbers into different arrays, then returns the single element from the smaller array:
const arr = [5, 9, 7, 11, 34, 23, 77];
const findDifferent = (arr = []) => {
const evens = [];
const odds = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] % 2 === 0) {
evens.push(arr[i]);
} else {
odds.push(arr[i]);
}
}
// Return the single element from the smaller array
return evens.length === 1 ? evens[0] : odds[0];
};
console.log(findDifferent(arr));
34
Optimized Solution
A more efficient approach stops as soon as we identify which type (even or odd) has only one element:
const arr1 = [5, 9, 7, 11, 34, 23, 77];
const arr2 = [2, 4, 6, 8, 15, 10, 12];
const findDifferentOptimized = (arr = []) => {
let evenCount = 0;
let oddCount = 0;
let lastEven, lastOdd;
// Count first 3 elements to determine pattern
for (let i = 0; i < Math.min(3, arr.length); i++) {
if (arr[i] % 2 === 0) {
evenCount++;
lastEven = arr[i];
} else {
oddCount++;
lastOdd = arr[i];
}
}
// Determine if we're looking for the single even or single odd
const lookingForEven = evenCount === 1;
// Find the target element
for (let i = 0; i < arr.length; i++) {
if (lookingForEven && arr[i] % 2 === 0) {
return arr[i];
} else if (!lookingForEven && arr[i] % 2 !== 0) {
return arr[i];
}
}
};
console.log("Array 1 result:", findDifferentOptimized(arr1));
console.log("Array 2 result:", findDifferentOptimized(arr2));
Array 1 result: 34 Array 2 result: 15
Using Array Filter Method
A functional programming approach using the filter() method:
const arr = [2, 4, 6, 8, 15, 10, 12];
const findDifferentWithFilter = (arr = []) => {
const evens = arr.filter(num => num % 2 === 0);
const odds = arr.filter(num => num % 2 !== 0);
return evens.length === 1 ? evens[0] : odds[0];
};
console.log(findDifferentWithFilter(arr));
15
Performance Comparison
| Method | Time Complexity | Space Complexity | Readability |
|---|---|---|---|
| Separate Arrays | O(n) | O(n) | Good |
| Optimized | O(n) | O(1) | Medium |
| Filter Method | O(n) | O(n) | Excellent |
Key Points
- The modulo operator
% 2determines if a number is even (result is 0) or odd (result is 1) - The optimized approach saves memory by not storing all elements
- Filter method provides the most readable solution using functional programming
- All approaches have O(n) time complexity but differ in space usage
Conclusion
Finding the single different element requires identifying whether you're looking for one even among odds or one odd among evens. The filter method offers the cleanest code, while the optimized approach provides better space efficiency for large arrays.
