Find longest occurrence of same number in array

前端 未结 12 1439
一生所求
一生所求 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:07

    Your problems:

    • You don't store current streak
    • You don't specify when streak is more then older streak

    Use this:

    function streak(arr) {
        var i,
            temp,
            streak = 1,
            maxStreak = 0,
            prevNumber,
            length = arr.length;
    
        for(i=1; i<length; i++) {
            prevNumber = arr[i-1];
            if (arr[i] == prevNumber) {
                streak += 1;
            } else {
                if(streak > maxStreak) {
                    maxStreak = streak;
                    streak = 1;
                }
            }
        }
        return maxStreak;
    }
    

    Demo

    0 讨论(0)
  • 2021-01-05 18:12

    Alternative: use regexp and converting the array to a string.

    var arr = [2,5,3,1,1,1,3,7,9,6,4,1,1,1,1,1,4,7,2,3,1,1,4,3];
    var str = arr.join('').match(/1+/g);
    console.log(process ? process.sort().pop() : "No ocurrences");
    
    0 讨论(0)
  • 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/

    0 讨论(0)
  • 2021-01-05 18:14

    You could take Array#reduce and return the start index of the actual same item sequence. Then check and update the counter if the item is not equal.

    var array = [2, 5, 3, 1, 1, 1, 3, 7, 9, 6, 4, 1, 1, 1, 1, 1, 4, 7, 2, 3, 1, 1, 4, 3],
        maxCount = 0,
        maxValues;
    
    array.reduce(function (j, a, i, aa) {
        if (aa[j] === a) {
            return j;
        }
        if (i - j === maxCount){
            maxValues.push(aa[j]);
        }            
        if (i - j > maxCount) {
            maxCount = i - j;
            maxValues = [aa[j]];
        }
        return i;
    }, -1);
    
    console.log(maxCount);
    console.log(maxValues);

    0 讨论(0)
  • 2021-01-05 18:15

    You will need another two arrays here.

    1. Store the distinct numbers from your source array using a loop
    2. Make a second set of array which is equal to the length of the first set of array which has the distinct numbers.
    3. Make a loop equal to the length of the first set of array and then push the values to the second set of array according to its index.
    4. Make a loop again using the second set of array and there you will find the most occurence using the index of the second array
    5. Finally, get from the first set of array the number using the index you got from step 4.

    I did not make the code for you to try it yourself first since you are asking only for some pointers

    0 讨论(0)
  • 2021-01-05 18:17

    You can use fewer iterations by looking ahead at all matches from a given index, and jumping ahead to the next non-matching item's index.

    You can also quit when there are less items left than the maximum you have found.

    function maxRepeats(arr){
        var L= arr.length, i= 0, 
        max= 1, count= 0;
        while(L-i > max){
            while(arr[i+count]=== arr[i])++count;
            if(count > max) max= count;
            i+= count;
            count= 0;
        }
        return max;
    }
    var A= [2, 5, 3, 1, 1, 1, 3, 7, 9, 6, 4, 1, 
    1, 1, 1, 1, 4, 7, 2, 3, 1, 1, 4, 3];
    

    maxRepeats(A); returns 5

    Finding multiple items that repeat the max number of times is not so easy, since you have to find the max number before you can list them. If you really only need the max number, ignore this:

    function mostRepeats(arr, maximum){
        var i= 0, max= maximum || 1, 
        L= arr.length-max, 
        count= 0, index= [];
        while(i<L){
            while(arr[i+count]=== arr[i])++count;
            if(count=== maximum) index.push(arr[i]+' starting at #'+i);
            else if(count > max) max= count;
            i+= count;
            count= 0;
        }
        if(max===1) return 'No repeats';
        return maximum? max+' repeats of: '+index.join(', '): mostRepeats(arr, max);
    }
    
    var A= [2, 5, 3, 1, 1, 1, 3, 7, 9, 6, 4, 1, 1, 1, 
    1, 1, 4, 7, 2, 3, 3, 3, 3, 3, 1, 1, 4, 3];
    

    mostRepeats(A);returns:

    5 repeats of: 1 starting at #11, 3 starting at #19

    0 讨论(0)
提交回复
热议问题