The text records Moses as having said,
"Now therefore kill every male among the little ones, and kill every woman that hath known man by lying with him." Numbers 31:17
The implication is that the women who had had sex were the ones who had engaged in the orgies with the men of Israel. This makes sense, since the Israelites were attacking only a group of Midianites, as indicated by the fact that there were Midianites who interacted with the Israelites after this event. But how would the Israelites have known which women had had sex and which women did not?