0

Given a collection [2,3,4,5,6,7] , how would you elegantly inject an arbitrary element between each element i.e. [2,1,3,1,4,1,5,1,6,1,7] ?

(However, [2] and [] should remain unchanged, because there is no 'between' when the element count is less than two.)

Party Ark
  • 1,061
  • 9
  • 20
  • See also [Intersperse a List<>](https://stackoverflow.com/questions/7430133/intersperse-a-list) – gunr2171 Mar 11 '23 at 15:03
  • 1
    If it were me, I wouldn't do it _in place_ (unless the spec was insistent that the list's object identity be maintained). Instead, I'd allocate a new List of the right length and then do a read-from-one + write-to-the-other loop. In all likelihood the list is going to do a lot of work internally (remember, it's backed by an array internally). Doing what I suggest would likely be less work. If you need to maintain object identity, do what I suggested, and when complete, `Clear` the original list and `AddRange` the new data in – Flydog57 Mar 11 '23 at 18:06

0 Answers0