Optimization in countFriendFoe method