E.g., if we have an array [1,2,3,4,6,7,8,15,16] then 6 and 15 are non-consecutive.
You should return the results as an array of objects with two values i: 'the index of the non-consecutive number' and n: 'the non-consecutive number'.
interfaceIndexValue{index:number;value:number;}/**
* Return all numbers with their index & value that are not following the others.
*
* @example
* getNonConsecutives([1, 2, 3]);
* getNonConsecutives([1, 2, 4]);
*/functiongetNonConsecutives(numbers:Readonly<Array<number>>):Array<IndexValue>{constnonConsecutives:Array<IndexValue>=[];constcount:number=numbers.length;for(letindex:number=1;index<count;index++){constcurrent:number=numbers[index];constprevious:number=numbers[index-1];constexpectedCurrent:number=previous+1;if(expectedCurrent===current){continue;}nonConsecutives.push({index,value:current});}returnnonConsecutives;}
Here's mine
Bravo! You did what I could not 😂
You could. You just need to go on step forward
for clarification, from the challenge:
solution in Nim:
Python solution 🐍
Here is the simple solution with Python:
Ruby
Haskell solution:
Test results:
TypeScript
Rust:
huhuhu iterator
BTW: the example is wrong.
{'i': 7, 'n': 15}should be{'i': 7, 'n': 10}- there is no15in the input.A python solution:
JavaScript solution in 5 lines
You could do it in one :)
SWI Prolog again
Ruby