I would like to determine whether a list contains any duplicate elements, while considering permutations as equivalent. All vectors are of equal length.
What is the most efficient way (shortest running time) to accomplish this?
## SAMPLE DATA
a <- c(1, 2, 3)
b <- c(4, 5, 6)
a.same <- c(3, 1, 2)
## BOTH OF THSE LISTS SHOULD BE FLAGGED AS HAVING DUPLICATES
myList1 <- list(a, b, a)
myList2 <- list(a, b, a.same)
# CHECK FOR DUPLICATES
anyDuplicated(myList1) > 0 # TRUE
anyDuplicated(myList2) > 0 # FALSE, but would like true.
For now I am resorting to sorting each member of the list before checking for duplicates
anyDuplicated( lapply(myList2, sort) ) > 0
I am wondering if there is a more efficient alternative. Also, in the ?duplicated
documentation, it indicates "Using this for lists is potentially slow". Are there other functions better suited for lists?
You could use
setequal
:What about this...?