This isn't a subtle or difficult-to-find case. It's a case of "why the heck would anyone write code like that, in any language, in the first place?" The only language-level abstraction needed to avoid this particular kind of duplicated code, is a loop.
How would you minimize these type of errors by design or best practice?
I guess languages with a lack of higher order abstractions and no strong type system might be more prone to this type of errors.