I always detested group punishment further the bad behavior of a few.
Sometimes they work though. At least to an extent.
There's sort of a range of "common misbehavior" that occurs in most social groups. Many times, punishing the single bad actor for something that seems like a small transgression can backfire, and the authority figure ends up bearing the ire of the entire group. But if the group as a whole is given a punishment that is inconvenient and disliked but not actually harmful, then the ire is turned on the group member who transgressed. It increases the likelihood that the group will enforce behavioral expectations itself. If done appropriately, it increases group cohesion and morale in a way that is net beneficial. It's a very common approach in the military, but it's not limited to that.
There's some art to it though, and it can backfire if the punishment is too harsh, or if the misbehavior is always a single individual.
Hypotheticals, of course
If there are 20 kids in a class, and 19 of them always follow the rules but 1 of them consistently and persistently talks back during class, calls the teacher and other kids bad names, and starts fights then it's likely NOT effective to punish the class as a whole. There's a single easily identifiable source of problem. In that case, it's more effective to punish only the problem kid, because it serves to protect and shelter the other kids from their actions.
On the other hand, if 6 of them talk during class and cause minor disruption, it's harder to tackle on a one-off basis. It ends up being a bit of whack-a-mole. If the teacher ends up in a position where they're frequently trying to redirect several students, then the teacher ends up being disruptive just by trying to do their job. On the other hand, if the teacher punishes the whole class with something that is inconvenient or unpleasant (taking away privileges or class clean-up stuff) then the teacher has to intervene much less often. The kids are more likely to be angry with the one who misbehaved in that instance. And they're more likely to self-police and apply social pressure on all of the kids (especially the 6 who are the most common offenders) in order to avoid unpleasantness for everyone.
It's something I've ended up dealing with at work over the past couple of years. Covid hit and everyone was working from home, and for the first couple of years it was fine, and productivity remained high. But then productivity started to drop, people not paying attention and not being responsive increased, work started to slip. At first, the direction from leadership was that it's on management to address it as a performance issue - taking it up with individuals on a one-off basis. But that didn't actually curb the problem, and it was widespread enough and dispersed enough in small infractions that it really couldn't be addressed effectively. So now everyone has to go into the office once a week - even those who were highly effective working from home without supervision. Everyone is unhappy about it, everyone feels like they're being punished for someone else's poor behavior. But productivity and responsiveness has increased back to a level where we can address the behavior of outliers as a performance issue. It sucks, but... "this is why we can't have nice things".