
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Returning only odd number from array in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of integers, arr, as the first and the only argument.
The array either consists of all even numbers and just one odd number or consists of all odd numbers and just one even number. Our function should return this one different element from the array.
For example, if the input to the function is −
Input
const arr = [5, 9, 7, 11, 34, 23, 77];
Output
const output = 34;
Output Explanation
Because the array consists of all odd numbers but 34 which is even.
Example
Following is the code −
const arr = [5, 9, 7, 11, 34, 23, 77]; const findDifferent = (arr = []) => { let { length: len } = arr, i; const evens = []; const odds = []; let k; for (i=0; i<len; i++) { if (arr[i] % 2 == 0) { evens.push(arr[i]); }; if (Math.abs(arr[i] % 2) == 1) { odds.push(arr[i]); }; }; if (evens.len > odds.len) return odds[0]; else return evens[0]; }; console.log(findDifferent(arr));
Output
34
- Related Articles
- Returning array values that are not odd in JavaScript
- Returning the highest value from an array in JavaScript
- Write a number array and add only odd numbers?
- Removing the odd occurrence of any number/element from an array in JavaScript
- Returning the highest number from object properties value – JavaScript
- Returning just greater array in JavaScript
- Returning an array containing last n even numbers from input array in JavaScript
- Returning number with increasing digits. in JavaScript
- Returning the first number that equals its index in an array using JavaScript
- Finding the only out of sequence number from an array using JavaScript
- Returning values from a constructor in JavaScript?
- Finding the only even or the only odd number in a string of space separated numbers in JavaScript
- Returning an Array from a Method in Java
- Accessing and returning nested array value - JavaScript?
- Returning reverse array of integers using JavaScript

Advertisements