This happens to every scientist at some point. We look at PubMed or examine the latest table of contents in Journal X and find a similar study. Often, there is no bad intention, just different labs in a similar field converging on the same topic. Naturally, this scenario can be very dispiriting to the student or postdoc from “second group” whose work is not yet published.
With Preprints: If the “second” group is close to the completion of their work, then they can post their findings on a preprint server. The “first” preprint or journal publication should be cited in this work. If the “second” work appears within a few weeks, it is clear that this work was performed independently as the time span between the two disclosures is too short for the “second group” to have initiated and completed the study de novo after the announcement of the first paper. Some journals offer “scoop protection” policies that honor the priority claims of preprints even if published literature has appeared in the intervening time.
Without Preprints: If the “second” group tries to make their disclosure in a journal, then the situation is less predictable. First, many journals might reject the work editorially or after peer review because it is too similar to already published work (even though it was performed independently and at approximately the same time). Furthermore, the journal publication will take time (potentially months), thus creating a larger time gap between the first and second study and making more ambiguous what the second group achieved independently at the time of the disclosure of the first group.