The sword refers to the number of times Offer JZ37 numbers appear in the sorted array (JavaScript:map)

Time limit: C/C++ 1 second, other languages ​​2 seconds Space limit: C/C++ 64M, other languages ​​128M Popularity index: 484815
Knowledge points of this question: Array dichotomy

Title Description
Count the number of times a number appears in the ascending array.
Example 1
Input
[1,2,3,3,3,3,4,5],3
returns the value
4

Idea: traverse, accumulate the results when they are equal

function GetNumberOfK(data, k)
{
    
    
    // write code here
    let res = 0
    for(let i = 0 ; i < data.length ; i++){
    
    
        if(data[i] === k){
    
    
            res++
        }
    }
    return res
}

Guess you like

Origin blog.csdn.net/weixin_44523860/article/details/115279408