最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

check if elements in array are consecutive --- javascript - Stack Overflow

programmeradmin4浏览0评论

I have an array as

arr = [1,2,3,4,6,7,8,9]

Now I want to check if the values in the array are consecutive.

Being more specific, I want this

First Check gives first and second element are consecutive and the next element is not consecutive then the algo must return the first element from where the consecutive number started

Like

First Check will give 1
Second Check will give 6
and so on...

Please help Thanks in advance

I have an array as

arr = [1,2,3,4,6,7,8,9]

Now I want to check if the values in the array are consecutive.

Being more specific, I want this

First Check gives first and second element are consecutive and the next element is not consecutive then the algo must return the first element from where the consecutive number started

Like

First Check will give 1
Second Check will give 6
and so on...

Please help Thanks in advance

Share Improve this question edited Nov 26, 2010 at 13:04 Rohit asked Nov 26, 2010 at 12:01 RohitRohit 5,7214 gold badges33 silver badges61 bronze badges 10
  • 4 Is this an assignment? It seems like it. If so, show what you've tried so far and where you are stuck. If not, could you explain the context that requires you to do this? – The Archetypal Paul Commented Nov 26, 2010 at 12:05
  • I want an algo which will return me the values as I described in the end – Rohit Commented Nov 26, 2010 at 12:06
  • @Rohit - What about a standalone number? for example should [1,2,3,4,6,8,9] return 1, 6, 8, or just 1, 8? – Nick Craver Commented Nov 26, 2010 at 12:08
  • have you tried a simple loop? – meo Commented Nov 26, 2010 at 12:09
  • if the array is [1,2,3,4,6,8,9] then it should return 1,6,8 i.e it should return the number from where we started getting the consecutive number – Rohit Commented Nov 26, 2010 at 12:09
 |  Show 5 more comments

6 Answers 6

Reset to default 5
/**
 * Given an array of number, group algebraic sequences with d=1
 * [1,2,5,4,8,11,14,13,12] => [[1,2],[4,5],[8],[11,12,13,14]]
 */
import {reduce, last} from 'lodash/fp';

export const groupSequences = (array) => (
  reduce((result, value, index, collection) => {
    if (value - collection[index - 1] === 1) {
      const group = last(result);
      group.push(value);
    } else {
      result.push([value]);
    }
    return result;
  }, [])(array)
);
 /**
 * Given an array of number, group algebraic sequences with d=1
 * [1,2,3,4,5,6] => true
 * [1,2,4,5,6] => false
 */
 const differenceAry = arr.slice(1).map(function(n, i) { return n - arr[i]; })
 const isDifference= differenceAry.every(value => value == 1)
 console.log(isDifference);

One sidenote is that you want to call it multiple times, so each call should know which array it's working on and what the previous offset in that array was. One thing you can do is to extend the native Array object. [Demo]

Array.prototype.nextCons = (function () {
  var offset = 0; // remember the last offset
  return function () {
    var start = offset, len = this.length;
    for (var i = start + 1; i < len; i++) {
      if (this[i] !== this[i-1] + 1) {
        break;
      }
    }
    offset = i;
    return this[start];
  };
})();

Usage

var arr =  [1,2,3,4,6,8,9];
arr.nextCons(); // 1
arr.nextCons(); // 6
arr.nextCons();​ // 8

Check if all numbers in array are consecutives:

Updated March 2022

 const allConsecutives = (arr) =>{ 
    if(arr.some(n=> typeof n !== "number" || Number.isNaN(n))) return false;
     return arr.every((num, i)=>  arr[i+1]-num === 1 || arr[i+1] === undefined)
  }

pseudo code:

int count = 0 
for i = 0 to array.length - 2 
    if  {array[i + 1] - array[i] = 1 then 
        count+=1 
         return i
    else count=0} 
const array1 = [1,2,3];
const sum = array1.reduce((accumulator, currentValue) =>{
  return accumulator + currentValue;
});
const max = Math.max(...array1);
  maximum = max
  if(sum == maximum * (maximum+1) /2) {
       console.log(true);
  } else {
       console.log(false);
  }
发布评论

评论列表(0)

  1. 暂无评论