Menu

gitpiper

isSorted javascript Code Snippet in 2024

arrayintermediate

Last Updated: 22 July 2024

Checks if a numeric array is sorted.

  • Calculate the ordering direction for the first pair of adjacent array elements.
  • Return 0 if the given array is empty, only has one element or the direction changes for any pair of adjacent array elements.
  • Use Math.sign() to covert the final value of direction to -1 (descending order) or 1 (ascending order).
const isSorted = arr => { if (arr.length <= 1) return 0; const direction = arr[1] - arr[0]; for (let i = 2; i < arr.length; i++) { if ((arr[i] - arr[i - 1]) * direction < 0) return 0; } return Math.sign(direction); };
isSorted([0, 1, 2, 2]); // 1 isSorted([4, 3, 2]); // -1 isSorted([4, 3, 5]); // 0 isSorted([4]); // 0

javascript snippet similar to isSorted For You in July 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️