I'm not going to review the code, but rather give an algorithmic hint.
If that was actually some programming contest, there will probably be some weird test cases in it. One of weirdest I can think of is a loooong list of equal values. It will make the code presented to scan the whole list, which takes a loooong linear time.
You could savereduce a time complexity by getting rid of linear scans and making two binary searches instead, with different conditions: one for the first element equal to the sought one, and one for the last equal.
Then you just test if the desired key was found and (if so) how far apart the two searches stopped.