Check if an Array is Sorted

medium

By - Aman Pareek

Last Updated - 27/08/2024

Problem Statement

Imagine you have a list of numbers, and you want to know if they’re arranged in a way that makes sense—specifically, if they’re sorted from smallest to largest, or at least not decreasing as you move through the list.

Your Task:

Write a function that checks if the numbers in a given list are in non-decreasing order. This means each number in the list should be less than or equal to the number that follows it.

Example 1

Input: array = [1, 2, 2, 3, 4]

Output: true

Example 2

Input: array = [10, 20, 30, 25, 40]

Output: false

Example 3

Input: array = [3, 2, 1]

Output: false

Example 4

Input: string = "hello"

Output: Throw error => Input is not a valid array

Solution 1: Using a Loop

function isArraySortedLoop(array) {
    if (!Array.isArray(array)) {
        throw new Error('Input is not a valid array');
    }

    for (let i = 1; i < array.length; i++) {
        if (array[i - 1] > array[i]) {
            return false;
        }
    }
    return true;
} 

const array1 = [1, 2, 2, 3, 4];
isArraySortedLoop(array1);  //output: true 

const array2 = [10, 20, 30, 25, 40];
isArraySortedLoop(array2);  //output: false 

const array3 = [3, 2, 1];
isArraySortedLoop(array3);  //output: false 

const string4 = "hello";
isArraySortedLoop(string4);  //Throw error => Input is not a valid array 

A hash map (or dictionary) is a great way to solve this problem eff…

Solution 2: Using every Method

function isArraySortedEvery(array) {
    if (!Array.isArray(array)) {
        throw new Error('Input is not a valid array');
    }

    return array.every((value, index, arr) => index === 0 || arr[index - 1] <= value);
} 

const array1 = [1, 2, 2, 3, 4];
isArraySortedEvery(array1);  //output: true 

const array2 = [10, 20, 30, 25, 40];
isArraySortedEvery(array2);  //output: false 

const array3 = [3, 2, 1];
isArraySortedEvery(array3);  //output: false 

const string4 = "hello";
isArraySortedEvery(string4);  //Throw error => Input is not a valid array 

Solution 3: Using reduce Method

function isArraySortedReduce(array) {
    if (!Array.isArray(array)) {
        throw new Error('Input is not a valid array');
    }

    return array.reduce((sorted, value, index) => {
        return sorted && (index === 0 || array[index - 1] <= value);
    }, true);
} 

const array1 = [1, 2, 2, 3, 4];
isArraySortedReduce(array1);  //output: true 

const array2 = [10, 20, 30, 25, 40];
isArraySortedReduce(array2);  //output: false 

const array3 = [3, 2, 1];
isArraySortedReduce(array3);  //output: false 

const string4 = "hello";
isArraySortedReduce(string4);  //Throw error => Input is not a valid array 

Solution 4: Using forEach Method

function isArraySortedForEach(array) {
    if (!Array.isArray(array)) {
        throw new Error('Input is not a valid array');
    }

    let sorted = true;
    array.forEach((value, index) => {
        if (index > 0 && array[index - 1] > value) {
            sorted = false;
        }
    });
    return sorted;
} 

const array1 = [1, 2, 2, 3, 4];
isArraySortedForEach(array1);  //output: true 

const array2 = [10, 20, 30, 25, 40];
isArraySortedForEach(array2);  //output: false 

const array3 = [3, 2, 1];
isArraySortedForEach(array3);  //output: false 

const string4 = "hello";
isArraySortedForEach(string4);  //Throw error => Input is not a valid array 

Solution 5: Using some Method

function isArraySortedSome(array) {
    if (!Array.isArray(array)) {
        throw new Error('Input is not a valid array');
    }

    return !array.some((value, index) => index > 0 && array[index - 1] > value);
} 

const array1 = [1, 2, 2, 3, 4];
isArraySortedSome(array1);  //output: true 

const array2 = [10, 20, 30, 25, 40];
isArraySortedSome(array2);  //output: false 

const array3 = [3, 2, 1];
isArraySortedSome(array3);  //output: false 

const string4 = "hello";
isArraySortedSome(string4);  //Throw error => Input is not a valid array 

Solution 6: Using find Method

function isArraySortedFind(array) {
    if (!Array.isArray(array)) {
        throw new Error('Input is not a valid array');
    }

    return array.find((value, index) => index > 0 && array[index - 1] > value) === undefined;
} 

const array1 = [1, 2, 2, 3, 4];
isArraySortedFind(array1);  //output: true 

const array2 = [10, 20, 30, 25, 40];
isArraySortedFind(array2);  //output: false 

const array3 = [3, 2, 1];
isArraySortedFind(array3);  //output: false 

const string4 = "hello";
isArraySortedFind(string4);  //Throw error => Input is not a valid array 

Solution 7: Using map Method

function isArraySortedMap(array) {
    if (!Array.isArray(array)) {
        throw new Error('Input is not a valid array');
    }

    return !array.map((value, index) => index > 0 && array[index - 1] > value).includes(true);
} 

const array1 = [1, 2, 2, 3, 4];
isArraySortedMap(array1);  //output: true 

const array2 = [10, 20, 30, 25, 40];
isArraySortedMap(array2);  //output: false 

const array3 = [3, 2, 1];
isArraySortedMap(array3);  //output: false 

const string4 = "hello";
isArraySortedMap(string4);  //Throw error => Input is not a valid array 

Solution 8: Using a Recursive Function

function isArraySortedRecursive(array, index = 0) {
    if (!Array.isArray(array)) {
        throw new Error('Input is not a valid array');
    }

    if (index >= array.length - 1) return true;
    if (array[index] > array[index + 1]) return false;
    return isArraySortedRecursive(array, index + 1);
} 

const array1 = [1, 2, 2, 3, 4];
isArraySortedRecursive(array1);  //output: true 

const array2 = [10, 20, 30, 25, 40];
isArraySortedRecursive(array2);  //output: false 

const array3 = [3, 2, 1];
isArraySortedRecursive(array3);  //output: false 

const string4 = "hello";
isArraySortedRecursive(string4);  //Throw error => Input is not a valid array 

Solution 9: Using JSON.stringify Comparison

function isArraySortedJSON(array) {
    if (!Array.isArray(array)) {
        throw new Error('Input is not a valid array');
    }

    const sortedArray = [...array].sort((a, b) => a - b);
    return JSON.stringify(array) === JSON.stringify(sortedArray);
} 

const array1 = [1, 2, 2, 3, 4];
isArraySortedJSON(array1);  //output: true 

const array2 = [10, 20, 30, 25, 40];
isArraySortedJSON(array2);  //output: false 

const array3 = [3, 2, 1];
isArraySortedJSON(array3);  //output: false 

const string4 = "hello";
isArraySortedJSON(string4);  //Throw error => Input is not a valid array 

Solution 10: Using a Helper Function to Compare Two Arrays

function isArraySortedByComparison(array) {
    if (!Array.isArray(array)) {
        throw new Error('Input is not a valid array');
    }

    const sortedArray = [...array].sort((a, b) => a - b);
    return arraysAreEqual(array, sortedArray);
}

function arraysAreEqual(a, b) {
    if (a.length !== b.length) return false;
    for (let i = 0; i < a.length; i++) {
        if (a[i] !== b[i]) return false;
    }
    return true;
} 

const array1 = [1, 2, 2, 3, 4];
isArraySortedByComparison(array1);  //output: true 

const array2 = [10, 20, 30, 25, 40];
isArraySortedByComparison(array2);  //output: false 

const array3 = [3, 2, 1];
isArraySortedByComparison(array3);  //output: false 

const string4 = "hello";
isArraySortedByComparison(string4);  //Throw error => Input is not a valid array 

Popular Solutions