w3resource

R Programming: Find all elements of a given list that are not in another given list

R Programming: List Exercise-21 with Solution

Write a R program to find all elements of a given list that are not in another given list.

Sample Solution :

R Programming Code :

l1 = list("x", "y", "z")
l2 = list("X", "Y", "Z", "x", "y", "z")
print("Original lists:")
print(l1)
print(l2)
print("All elements of l2 that are not in l1:")
setdiff(l2, l1)

Sample Output:

[1] "Original lists:"
[[1]]
[1] "x"

[[2]]
[1] "y"

[[3]]
[1] "z"

[[1]]
[1] "X"

[[2]]
[1] "Y"

[[3]]
[1] "Z"

[[4]]
[1] "x"

[[5]]
[1] "y"

[[6]]
[1] "z"

[1] "All elements of l2 that are not in l1:"
[[1]]
[1] "X"

[[2]]
[1] "Y"

[[3]]
[1] "Z"                         

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a R program to get the length of the first two vectors of a given list.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.