
- 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
Special arrays in JavaScript
An array is said to be a special array if there exists a positive integer num, such that there are num elements greater than num in the array. The number num does not necessarily have to belong to the array, it should just exist.
For example − If the input array is −
const arr = [2, 1, 5, 2, 7, 9];
Then, if we examine properly, we will find that for num = 3, there are exactly 3 elements greater than 3 in this array.
However, 3 is not a part of this array, but that's valid. Therefore, we are required to write a JavaScript function that takes in an array of Numbers.
The function should return the number around which it is special, (if it is special at all), otherwise the function should return -1.
Example
const arr = [2, 1, 5, 2, 7, 9]; const findSpecialArray = (array = []) => { const arr = array.slice().sort((a, b) => a - b); let index = 1; const { length } = arr; while(index <= arr[length-1]){ let num = 0; for(let i=0; i<length; i++){ if(arr[i] >= index){ num++; } }; if(num === index){ return index; }; index++; }; return -1; }; console.log(findSpecialArray(arr));
Output
This will produce the following output −
3
- Related Articles
- Checking for special type of Arrays in JavaScript
- Sum of special triplets having elements from 3 arrays in C++
- Finding special array - JavaScript
- Checking for special numbers in JavaScript
- Special type of numbers (pronic) in JavaScript
- Special type of sorting algorithm in JavaScript
- Chunking arrays in JavaScript
- Flattening arrays in JavaScript
- Deviations in two JavaScript arrays in JavaScript
- Finding special type of numbers - JavaScript
- Finding special kind of sentences (smooth) in JavaScript
- Arrays Data Structure in Javascript
- Joining two Arrays in Javascript
- Multi Dimensional Arrays in Javascript
- Arrays vs Set in JavaScript.

Advertisements