Find longest occurrence of same number in array

前端 未结 12 1520
一生所求
一生所求 2021-01-05 17:49

Using JavaScript, I\'m trying to find a way to find the longest occurrence of the same number (in this case, 1) in an array.

For instance, here\'s a sample array:

12条回答
  •  庸人自扰
    2021-01-05 18:13

    Unfortunately I can't comment yet due to lack of reputation so I will post this as an answer. For my task Robbie Averill's solution was perfect, but it contains a little bug. I had array that consisted of 2 values - 0 & 1.5, but above-mentioned code was counting only "1.5" values although I had "0" repeating in a higher streak. Problem was that value wasn't doing strict comparison here:

    if(temp != '' && temp == arr[i]) {
    

    and the fix was simple: if(temp !== '' && temp == arr[i]) {

    I've updated Robbie's jsfiddler with this fix: http://jsfiddle.net/d5X2k/5/

提交回复
热议问题