Find the longest sequential sequence of numbers in an array (JavaScript implementation)

Source: Internet
Author: User

Original topic:

Given an unordered sequence of integers, the longest sequential sequence of numbers is found.

For example:

Given [100, 4, 200, 1, 3, 2],

The longest sequential sequence of numbers is [1, 2, 3, 4].

A solution given by a vegetable dish:

1 functionmaxsequence (array,step) {2   var_array = Array.slice (),//Clone Array3_step = 1,4_arraytemp = [],5i = 0;6   7   varParselogic = {8     //result Container9 parseresults: [],Ten     //set value to Array,what ' s the last array of Parseresults OneSetfunction(n) { A        This. parseresults[ This. parseresults.length-1].push (n); -     }, -     //get The last array from Parseresults theGetfunction(){ -       return  This. parseresults[ This. parseresults.length-1]; -     }, -     //put a new array in Parseresults +AddItem:function(){ -        This. Parseresults.push ([]); +     }, A     //Sort Parseresults atSORTBYASC:function(){ -        This. Parseresults.sort (function(A, b) { -         returnA.length-b.length; -       }); -     } -   }; in    -   //Check params to_step = Step | |_step; +    -   //sort array by ASC the_array.sort (function(A, b) { *     returnAb; $   });Panax Notoginseng    -   //Remove repeat of data the    for(i = 0;i<_array.length;i++){ +     if(_array[i]! = _array[i+1]){ A _arraytemp.push (_array[i]); the     } +   } -_array =_arraytemp.slice (); $_arraytemp = []; $    -   //Parse Array - Parselogic.additem (); the    for(i = 0;i<_array.length;i++){ -     if(_array[i]+_step = = _array[i+1]){Wuyi Parselogic.set (_array[i]); the       Continue; -     } Wu     if(_array[i]-_step = = _array[i-1]){ - Parselogic.set (_array[i]); About Parselogic.additem (); $     } -   } -    -   //Sort Result A Parselogic.sortbyasc (); +    the   //get the max sequence -   returnparselogic.get (); $    the}
View Code

Invocation Description:

Method Name:

Maxsequence (Array,step)

parameter Description:

Array: the arrays to find. Necessary.

Step: Sequence Step (increment). Optional, default is 1.

return Value:

This method does not change the incoming array, and it returns a new array that contains the largest sequence.

Invocation Example:

Maxsequence ([5,7,2,4,0,3,9],1); return [2,3,4,5]

Maxsequence ([5,7,2,4,0,3,9],2); return [5,7,9]

Find the longest sequential sequence of numbers in an array (JavaScript implementation)

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.