[Optimization] Replace O(N) operation with O(1).