Check if Given Array is Monotonic in JavaScript
Last Updated :
22 Feb, 2024
To determine whether an array is monotonic that is completely non-increasing or non-decreasing an array should have members ordered either ascending or descending in a direction-neutral fashion that is called monotone. A successful solution to this problem can simplify several algorithmic tasks.
In JavaScript, there are several ways to determine if an array is monotonic or not which are as follows:
Using single-pass comparison
To ascertain whether the array is monotonic, this method traverses the array once and compares each element with its neighbouring element. As it loops over the array, it updates the two flags it maintains—one for non-increasing and one for non-decreasing.
Example: Function to determine if an array is either entirely non-increasing or non-decreasing, providing a quick monotonicity check.
Javascript
function isMonotonic(array) {
let isNonIncreasing = true ;
let isNonDecreasing = true ;
for (let i = 1; i < array.length; i++) {
if (array[i] > array[i - 1]) {
isNonIncreasing = false ;
}
if (array[i] < array[i - 1]) {
isNonDecreasing = false ;
}
}
return isNonIncreasing || isNonDecreasing;
}
console.log(isMonotonic([1, 2, 2, 4]));
console.log(isMonotonic([3, 2, 5]));
console.log(isMonotonic([1, 3, 9]));
|
Using sorting
The array’s elements are initially rearranged in either ascending or descending order by sorting the array. Then you can check if it’s monotonic by comparing the sorted array with the original. However, because of sorting, this method modifies the original array and has an O(n log n) time complexity.
Example: To demonstrate using sorting to check if an array is monotonic, comparing the original array with its ascending and descending sorted versions for both non-decreasing and non-increasing conditions.
Javascript
function isMonotonic(array) {
const sortedArray = [...array].sort((a, b) => a - b);
return array.every((value, index) =>
value === sortedArray[index])
||
array.every((value, index) =>
value === sortedArray[array.length - 1 - index]);
}
console.log(isMonotonic([1, 2, 2, 7]));
console.log(isMonotonic([3, 2, 9]));
console.log(isMonotonic([1, 3, 4]));
|
Using set comparison
Duplicates are immediately eliminated by turning the array into a Set, leaving unique elements. You can find out if the array is monotonic by looking at the size of the generated Set. Nevertheless, this method necessitates extra room in line with the array’s size.
Example: JavaScript function to assess array monotonicity by checking the count of unique values. It returns true
if there are either one or two unique values, representing a monotonic sequence.
Javascript
function isMonotonic(array) {
const uniqueValues = new Set(array);
return uniqueValues.size === 1
||
uniqueValues.size === 2;
}
console.log(isMonotonic([1, 2, 2, 7]));
console.log(isMonotonic([3, 2, 7]));
console.log(isMonotonic([1, 3, 7]));
|
Using functional programming
This approach employs higher-order array methods like reduce() to iteratively check if the array meets monotonic conditions. It emphasizes a functional programming paradigm, leveraging concise and expressive code.
Example: JavaScript function utilizing the reduce
method to iteratively check if an array is monotonic by evaluating each element against its preceding one, ensuring either non-decreasing or non-increasing conditions hold throughout the array.
Javascript
function isMonotonic(array) {
return array.reduce((acc, curr, index) =>
{
if (index === 0) return acc;
return acc && (curr >= array[index - 1]
||
curr <= array[index - 1]);
}, true );
}
console.log(isMonotonic([1, 2, 2, 8]));
console.log(isMonotonic([3, 2, 7]));
console.log(isMonotonic([1, 3, 8]));
|
Using recursion
This method involves iteratively determining whether the first two elements provide a monotonic trend. If they do, the function is executed on the remaining array recursively. Although this method is straightforward, it may result in performance overhead from several function calls.
Example: JavaScript function using recursion to check if an array is monotonic by comparing the first two elements and applying it recursively to check on the remaining array, ensuring a monotonic sequence.
Javascript
function isMonotonic(array) {
if (array.length <= 2) return true ;
return (
(array[0] <= array[1]
&& isMonotonic(array.slice(1)))
||
(array[0] >= array[1]
&& isMonotonic(array.slice(1)))
);
}
console.log(isMonotonic([1, 2, 2, 6]));
console.log(isMonotonic([3, 2, 7]));
console.log(isMonotonic([1, 3, 8]));
|
Using Math(Sign Analysis)
By calculating the difference between adjacent elements, you can determine if the array is monotonic. If all differences have the same sign (positive or negative), the array is monotonic. This approach is straightforward and efficient in terms of time complexity.
Example: Using two flags to track increasing and decreasing conditions in an array, determining if it is monotonic—either entirely non-increasing or non-decreasing.
Javascript
function isMonotonic(array) {
let isIncreasing = true ;
let isDecreasing = true ;
for (let i = 1; i < array.length; i++) {
if (array[i] > array[i - 1]) {
isDecreasing = false ;
}
if (array[i] < array[i - 1]) {
isIncreasing = false ;
}
}
return isIncreasing || isDecreasing;
}
console.log(isMonotonic([1, 2, 2, 5]));
console.log(isMonotonic([3, 2, 5]));
console.log(isMonotonic([1, 3, 4]));
|
Share your thoughts in the comments
Please Login to comment...