I want to keep a track of top 5 token holders inside the contract in terms of their token balances, is there any efficient way to do that in solidity?
You could store data in a heap which would approximately double your storage requirements (or less if you're willing to do some extra computation for indirection) for balances but gives you
O(log n) changes and you can get the top 5 holders in
O(log n). You might also consider a sorted list for the entirety of the list (not just the top 5); insertions/deletions are
O(log n) and lookups are
O(1). In all cases here,
n is the number of token holders.