- 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
Finding array number that have no matching positive or negative number in the array using JavaScript
Problem
We are required to write a JavaScript function that takes in an array of integers. For each number in the array there will also be its negative or positive compliment present in the array, but for exactly one number, there will be no compliment.
Our function should find and return that number from the array.
Example
Following is the code −
const arr = [1, -1, 2, -2, 3]; const findOddNumber = (arr = []) => { let count = 0; let number = arr.reduce((total, num) => { if (num >= 0) count++ else count-- return total + num; }, 0) return number / Math.abs(count); }; console.log(findOddNumber(arr));
Output
3
- Related Articles
- Finding unlike number in an array - JavaScript
- Finding matching pair from an array in JavaScript
- Why is there no zero positive and negative number?
- JavaScript Finding the third maximum number in an array
- Finding confusing number within an array in JavaScript
- Sum a negative number (negative and positive digits) - JavaScript
- Finding the only out of sequence number from an array using JavaScript
- Finding the nth missing number from an array JavaScript
- Finding the third maximum number within an array in JavaScript
- Test array values for positive or negative infinity in Numpy
- Finding the largest non-repeating number in an array in JavaScript
- Finding the first non-consecutive number in an array in JavaScript
- Finding two closest elements to a specific number in an array using JavaScript
- Finding a greatest number in a nested array in JavaScript
- Positive, negative and zeroes contribution of an array in JavaScript

Advertisements