update to only loop once instead of iterating twice over the same array.
Merge pull request #16 from limianwang/master

perf update: only iterate once over array.