Both Ezra 2:64 and Nehemiah 7:66 agree that the totals for the whole assembly was 42,360, yet Ezra adds up to 29,818 and Nehemiah to 31,089?
Ezra gives a detailed listing of families of Judah, Benjamin, the priests, and the Levites. (Ezra 1:5). That listing is from Ezra 2:1-67. Thus there were 29,818 listed plus an additional 12,542 people who came with them. In addition, Ezra's list is of those who enrolled to go to Israel. In other words, this was the list of intended travelers.
Nehemiah has a list of those who arrived in Jerusalem. Thus, there are small differences in numbers as some would die and others would join or have been born during the journey. It is also possible that some changed their minds and didn't complete the journey while others joined even though they had not registered their intention. However, the total still came to the same amount: 42,360 people. Nehemiah's detailed list covers 31,089 men, leaving an additional 11,271 "miscellaneous" people.
The detailed counts covered only the men (Ezra 2:2; Nehemiah 7:7) divided into 32 family groups. The remainder were likely the men from other tribes who were not considered a part of Judah before the captivity. Nehemiah's list may have a greater number in the detailed account because the male children grew up during the journey and were then counted as adults after the arrival.
Finally, we need to note that Nehemiah's list is a record of a found genealogy (Nehemiah 7:5). Nothing was said that it was an inspired listing, so the found list might have contained errors in the detailed count. There are differences in the family names, but that is easily explained because of variant spellings or the use of alternative names (some people went by multiple names).