Last Occurrence using binary search

Why this approach for finding last occurrence of an element failing for array inputs like: 1,2,3,4,2

https://ide.codingblocks.com/s/147215

This is a simple binary search algo and it works only on sorted arrays. Your input is not sorted. And this algo will only find the first occurrence it comes across. So use linear search towards the right when you find an element and if there is no element on the right, return the original element as the last occurence.