Might be easier to loop starting from the end of the object array to count the trailing nulls. Then you know how many "significant" elements there are. Pass object and the numNonNullElements into estimateByteSize. Then I don't think you'd need the Pair at all.