Many times when we need to display a collection ie array, list, set, vector to the end user, we have to insert comma between the elements. There are several conditions to satisfy before really inserting comma,
1. There should be more then 1 value in the collection.
2. There should not be any comma after the last value.
3. Only intermediate values should have commas.
4. If printing arrays, there must not be any bracket.
The optimized solution for set is
The optimized solution for list/array is