Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check array in JS - is list sorted? [duplicate]

Tags:

I need to create a program that checks the list in the array is sorted. I have three input data:

1,2,3,4,5

1,2,8,9,9

1,2,2,3,2

So here is my code:

let sorts = +gets(); // 3 let list = [];  for (let i = 0; i < sorts; i++) {     list[i] = gets().split(',').map(Number); // The Array will be: [ [ 1, 2, 3, 4, 5 ], [ 1, 2, 8, 9, 9 ], [ 1, 2, 2, 3, 2 ] ] }  for (let i = 0; i < list[i][i].length; i++){     if (list[i][i] < list[i][i +1]) {         print('true');     } else {         print('false');     } } 

I need to print for all lists on new line true or false. For this example my output needs to be:

true

true

false

I have no idea how to resolve this.

like image 559
Николай Матев Avatar asked Dec 18 '18 12:12

Николай Матев


People also ask

How do you check if there is a duplicate in an array Javascript?

To check if an array contains duplicates: Use the Array. some() method to iterate over the array. Check if the index of the first occurrence of the current value is NOT equal to the index of its last occurrence. If the condition is met, then the array contains duplicates.

How do you check if array is already sorted JS?

Logic : You can take the first item and second item and subtract the value. If second item minus the first item is positive, they are sorted. Now you can can move the index forward and check the next two and similarly.

How do I check if an array contains duplicates?

function checkIfArrayIsUnique(myArray) { for (var i = 0; i < myArray. length; i++) { for (var j = i+1; j < myArray. length; j++) { if (myArray[i] == myArray[j]) { return true; // means there are duplicate values } } } return false; // means there are no duplicate values. }

Does array sorted remove duplicates?

We can remove duplicate element in an array by 2 ways: using temporary array or using separate index. To remove the duplicate element from array, the array must be in sorted order. If array is not sorted, you can sort it by calling Arrays. sort(arr) method.


2 Answers

How about something like this:

!![1,2,3,4,5].reduce((n, item) => n !== false && item >= n && item) // true  !![1,2,8,9,9].reduce((n, item) => n !== false && item >= n && item) // true   !![1,2,2,3,2].reduce((n, item) => n !== false && item >= n && item) // false 

Reduce will literally reduce the array down to a single value - a boolean in our case.

Here, we are calling a function per iteration, the (n, item) is our function signature, it's body being n !== false && item >- n && item - we are making sure that n exists (n is our accumulator - read up!), testing if item is greater than n, and making sure item exists.

This happens for every element in your array. We then use !! to force the result into a tru boolean.

like image 174
Stuart Avatar answered Sep 18 '22 19:09

Stuart


You can use array#every to check if each value is greater than the previous value.

const isSorted = arr => arr.every((v,i,a) => !i || a[i-1] <= v);  console.log(isSorted([1,2,3,4,5]));  console.log(isSorted([1,2,8,9,9]));   console.log(isSorted([1,2,2,3,2]));
like image 26
Hassan Imam Avatar answered Sep 18 '22 19:09

Hassan Imam