4

I read this, but it doesn't apply (and/or, I can't figure out how to adapt the solutions). I also found this, but I don't want to change the array - I just want to check the information. I was unable to adapt the solutions to fit my needs.

I want to find out of the values in a Javascript Array are Sequential.

For example - I have an array of UNIX timestamps

var ts = [1451772000, 1451858400, 1452031200]

I want to return true if they are sequential (lower to higher values) and false if they are not sequential. I would also like to return false if there are duplicate values.

4
  • With just javascript (no libraries) you'll have to brute force and compare each two values. I'm unaware of any libraries that do this, since you could solve it in just 5 lines of code or less... Commented Jan 11, 2016 at 14:28
  • 2
    @ecc: Any library is just going to brute force it anyway. There is no way to do this that isn't going to be O(n), or O(1) in the best-case (the very first pair of values are in the wrong order). Commented Jan 11, 2016 at 14:30
  • Yes, I am aware. It was more of "a more elegant way" to solve it than with vanilla JS. Commented Jan 11, 2016 at 14:31
  • Loop through the array from 0 to n-1 and for each element compare the ith element to the i+1 element with <. If that's false, quit early and return false (they aren't in the right order). If you make it to the end of the array, then they are all in order, return true. Commented Jan 11, 2016 at 14:32

3 Answers 3

14

You can use Array.prototype.every, like this

var data = [1451772000, 1451858400, 1452031200];
console.log(data.every((num, i) => i === data.length - 1 || num < data[i + 1]));

The same can be written with a normal function, like this

console.log(data.every(function(num, index) {
  return index === data.length - 1 || num < data[index + 1];
}));

There are basically only two conditions to take care here

  1. If we reached the last index, then all the elements are good.

  2. If it is not the last element, then the current number should be strictly lesser than the next element.

This expression takes care of the above two conditions.

i === data.length - 1 || num < data[i + 1]

The every function calls the function passed to it, for each and every value of the array, with three parameters.

  1. current element,
  2. current index
  3. the actual array

It will keep calling the function, till the array elements run out or any of the calls to the function returns a falsy value.

Sign up to request clarification or add additional context in comments.

Comments

3

You can use simple for-loop like this

function isSequential(data) {
  for (var i = 1, len = data.length; i < len; i++) {
    // check if current value smaller than previous value
    if (data[i] < data[i - 1]) {
      return false;
    }
  }
  
  return true;
}

console.log(isSequential([1]));
console.log(isSequential([1, 2, 3, 4]));
console.log(isSequential([1, 5, 3, 4]));
console.log(isSequential([1451772000, 1451858400, 1452031200]));

Comments

2

This works on any length and prevents the first element to check.

function isSequential(array) {
    return array.every(function (a, i, aa) {
        return !i || aa[i - 1] < a;
    });
}

document.write(isSequential([42]) + '<br>');
document.write(isSequential([1, 2, 3, 4]) + '<br>');
document.write(isSequential([1, 5, 3, 4]) + '<br>');
document.write(isSequential([1451772000, 1451858400, 1452031200]) + '<br>');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.