In R, you can convert both lists to sets and compare them to check if they have the same elements regardless of order.
In this example,
list1
and list2
with the same set of elements.unique()
function.identical()
function.list1 <- c(1, 2, 3)
list2 <- c(3, 2, 1)
result <- identical(unique(list1), unique(list2))
print(paste("Do the lists have the same elements?", result))
Do the lists have the same elements? TRUE
In this example,
list1
and list2
with different sets of elements.unique()
function.identical()
function.list1 <- c(1, 2, 3)
list2 <- c(4, 5, 6)
result <- identical(unique(list1), unique(list2))
print(paste("Do the lists have the same elements?", result))
Do the lists have the same elements? FALSE
In this tutorial, we learned How to check if Two Lists have Same Elements (Regardless of Order) in R language with well detailed examples.