You start off with n elements as the size of the array. If there are any duplicates, after removing them, the size of the array would surely be less than n, but you did not update the value of n, hence you get those "garbage values".Originally Posted by Punju
It looks like you declared duplicate_elements as having a return type, but you did not actually return a value. You might as well return the new effective size of the array so that you can update n in main.Originally Posted by Punju
By the way, duplicate_elements doesn't sound like a good name for the function: it sounds like you're duplicating elements rather than removing duplicates. A name like remove_duplicate_elements or even more simply remove_duplicates would be better.