- 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 a number and its nth multiple in an array in JavaScript
We are required to write a JavaScript function that takes in an array of integers as the first argument and a number, say n, as the second argument.
The function should check whether there exists two such numbers in the array that one is the nth multiple of the other.
If there exists any such pair in the array, the function should return true, false otherwise.
For example −
If the array and the number are −
const arr = [4, 2, 7, 8, 3, 9, 5]; const n = 4;
Then the output should be −
const output = true;
because there exist the numbers 2 and 8 in the array and.
8 = 2 * 4
Example
Following is the code −
const arr = [4, 2, 7, 8, 3, 9, 5]; const n = 4; const containsNthMultiple = (arr = [], n = 1) => { const hash = new Set(); for(let i = 0; i < arr.length; i++){ const el = arr[i]; const [left, right] = [el / n, el * n]; if(hash.has(left) || hash.has(right)){ return true; }; hash.add(el); }; return false; }; console.log(containsNthMultiple(arr, n));
Output
Following is the console output −
true
- Related Articles
- Finding the nth missing number from an array JavaScript
- Finding the nth prime number in JavaScript
- Finding unlike number in an array - JavaScript
- Finding confusing number within an array in JavaScript
- Finding all duplicate numbers in an array with multiple duplicates in JavaScript
- JavaScript Finding the third maximum number in an array
- Finding sum of every nth element of array in JavaScript
- Finding the nth palindrome number amongst whole numbers in JavaScript
- Finding the third maximum number within an array in JavaScript
- Finding the largest non-repeating number in an array in JavaScript
- Finding the first non-consecutive number in an array in JavaScript
- Finding the nth element of the lucas number sequence in JavaScript
- Finding the largest and smallest number in an unsorted array of integers in JavaScript
- Finding the nth power of array element present at nth index using JavaScript
- Finding a greatest number in a nested array in JavaScript

Advertisements