

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- Sum a negative number (negative and positive digits) - JavaScript
- Finding unlike number in an array - JavaScript
- JavaScript Finding the third maximum number in an array
- Test array values for positive or negative infinity in Numpy
- Finding matching pair from an array in JavaScript
- Finding the matching number of a graph
- Finding confusing number within an array in JavaScript
- Finding the nth missing number from an array JavaScript
- How to check if a number is positive, negative or zero using Python?
- Java program to find if the given number is positive or negative
- Check if a number is positive, negative or zero using bit operators in C++
- Finding the third maximum number within an array in JavaScript
- Finding the only out of sequence number from an array using JavaScript
- Java Program to Check Whether a Number is Positive or Negative
- Positive, negative and zeroes contribution of an array in JavaScript
Advertisements