Function for handling duplicate values in data