Sebastian's one is surely a more compact solution and probably more efficient. But it doesn't deal with repeated notes, better said repeated MIDI notes with the same MIDI note number, as the Hans' one does. best
Are you sure? Sebastian's solution is the one with [bag]? Without checking myself I thought that [bag] can deal with multiple copies of the same number being stored. And trying its helpfile shows it is so.