Find most occurred number in sorted array of numbers
You should always question the question when given problems like this. For example, what if the input data is empty? What if there are two different values that appear the same number of times? etc. What if there are negative values?

Also, when dealing with problems like this its often best to separate the function doing the work, from the code presenting the result.

I would return an object from the function so that the "calling" code can look like:

var result = maxVal(data)
if (result.count) {
    console.log("NUM "+result.value+" occurred "+result.count+" times");
} else {
    console.log("No values in the data");
}


Then, I would also simplify your logic a whole bunch. You don't need to keep all those nested if statements.... You need just one test condition, and then an if/else condition...

function findMaxOccuredNum(nums){
    var result = {
        count: 0,
        value: 0
    };

    if (!nums) {
        return result;
    }

    var value = 0;
    var count = 0;

    for (var i = 0; i &lt; nums.length; i++) {
        if (nums[i] != value) {
            count = 0
            value = nums[i];
        }
        count++;
        if (count &gt; result.count) {
            result.count = count;
            result.value = value;
        }
    }
    return result;
}




function findMaxOccuredNum(nums){
        var result = {
            count: 0,
            value: 0
        };

        if (!nums) {
            return result;
        }
    
        var value = 0;
        var count = 0;
    
        for (var i = 0; i &lt; nums.length; i++) {
            if (nums[i] != value) {
                count = 0
                value = nums[i];
            }
            count++;
            if (count &gt; result.count) {
                result.count = count;
                result.value = value;
            }
        }
        return result;
    }

    function format(result) {
        return `Value ${result.value} &amp; Count ${result.count}`;
    }
Result: &lt;input id="output" type="text" cols="90"&gt;
&lt;p&gt;
&lt;input type="button" onClick="alert(format(findMaxOccuredNum([0,1,2,3,4,4,5])));" value="[0,1,2,3,4,4,5]"&gt;
&lt;input type="button" onClick="alert(format(findMaxOccuredNum([0,0,0,0,,1,2,3,4,4,5])));" value="[0,0,0,0,,1,2,3,4,4,5]"&gt;
&lt;input type="button" onClick="alert(format(findMaxOccuredNum([-1,-1,-1,0,1,2,4,4,5])));" value="[-1,-1,-1,0,1,2,4,4,5]"&gt;
&lt;input type="button" onClick="alert(format(findMaxOccuredNum([0,1,2,3,4,5])));" value="[0,1,2,3,4,5]"&gt;
&lt;input type="button" onClick="alert(format(findMaxOccuredNum([0,1,2,3,4,4,5])));" value="[0,1,2,3,4,4,5]"&gt;
&lt;input type="button" onClick="alert(format(findMaxOccuredNum([])));" value="[]"&gt;
&lt;input type="button" onClick="alert(format(findMaxOccuredNum()));" value="null"&gt;