I would use table(). Table will give you a list of all values and the number of times they occur.
vec <- c(1,2,3,4,5,3,4,5,4,5)
valueset <- unique(vec) # all unique values in vec, will be used later
#now we determine whihc values are occuring more than once
valuecounts <- table(vec) # returns count of all unique values, values are in the names of this variable as strings now regardless of original data type
morethanone <- names(valuecounts)[valuecounts>1] #returns values with count>1
morethanone <- as.numeric(morethanone) # converts strings back to numeric
valueset[valueset %in% morethanone] #returns the original values which passed the above cirteria
As a function....
duplicates <- function(vector){
# Returns all unique values that occur more than once in a list
valueset <- unique(vector)
valuecounts <- table(vector)
morethanone <- names(valuecounts)[valuecounts>1]
morethanone <- as.numeric(morethanone)
return( valueset[valueset %in% morethanone] )
}
Now try running duplicates(x)