- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Picking the odd one out in JavaScript
We are required to write a JavaScript function that takes in an array of literals that contains all similar elements but one.
Our function should return the unlike number.
Therefore, let’s write the code for this function −
Example
The code for this will be −
const arr = [2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]; // considering that the length of array is atleast 3 const findUnlike = arr => { for(let i = 1; i < arr.length-1; i++){ if(arr[i] - arr[i-1] !== 0 && arr[i]-arr[i+1] === 0){ return arr[i-1]; }else if(arr[i] - arr[i-1] !== 0 && arr[i]-arr[i+1] === 0){ return arr[i] }else if(arr[i] - arr[i-1] === 0 && arr[i]-arr[i+1] !== 0){ return arr[i+1]; }; continue; }; }; console.log(findUnlike(arr));
Output
The output in the console will be −
2
- Related Articles
- Picking out uniques from an array in JavaScript
- Find the odd one out from the following.TawaSpadePressure cookerEraser
- Picking the largest elements from multidimensional array in JavaScript
- Picking index randomly from array in JavaScript
- Picking all the numbers present in a string in JavaScript
- Picking the triangle edges with maximum perimeter JavaScript
- Select the ODD one out :1) cotton 2) wool3) rayon 4) silk. Give Reason.
- Find the odd one out:(i) Intestine  (ii) Appendix  (iii) Wisdom teeth  (iv) Coccyx
- Picking all elements whose value is equal to index in JavaScript
- How to find the one integer that appears an odd number of times in a JavaScript array?
- Take an array and find the one element that appears an odd number of times in JavaScript
- Explain cherry picking in Git
- Separate odd and even in JavaScript
- Fetching odd appearance number in JavaScript
- Reverse only the odd length words - JavaScript

Advertisements