Check If One List Contains Element from the Other

Check if one list contains element from the other

If you just need to test basic equality, this can be done with the basic JDK without modifying the input lists in the one line

!Collections.disjoint(list1, list2);

If you need to test a specific property, that's harder. I would recommend, by default,

list1.stream()
.map(Object1::getProperty)
.anyMatch(
list2.stream()
.map(Object2::getProperty)
.collect(toSet())
::contains)

...which collects the distinct values in list2 and tests each value in list1 for presence.

Check if one list contains element from the other in groovy

Depending on your list objects/maps there your approach would only work if the objects themself are compareable (which they are most likely not). So it might be easier to just build an index of the known items for a quick lookup. E.g.

def knownCarsIds = cars*.uuid.toSet()

Then only add if such an id ist not known:

...
if (!knownCarIds.contains(it.uuid)
...

Check if list contains element different from something

For the bare boolean check, you can do sth fancy like this, using map and any:

for lst in (a, b, c):
print(any(map("_".__ne__, lst)))
# False
# True
# True

Or be a little more explicit:

any(x != "_" for x in lst)

any has the advantage of short-circuiting (stopping at the first hit) as compared to your suggested set length approach:

len(set(lst)) > 1

If that makes it faster in the real world depends on your data. If most your lists are only underscores and therefore iterated in their entirety, the set conversion (C-optimized) may well be faster.

How to check if a list contains all the elements of another list INCLUDING duplicates

Repeated calls to list.count (for the same list) are very inefficient. You can use collections.Counter and how it implements differences:

from collections import Counter

def contains(l1, l2):
return not (Counter(l2) - Counter(l1))

>>> contains(["A", "A", "A", "b", "b"], ["A", "A", "b", "b"])
True
>>> contains(["a", "a", "b"], ["a", "b"])
True
>>> contains(["a", "b"], ["a", "a", "b"])
False
>>> contains(["a", "a", "b"], ["a", "b", "c"])
False

Check if a list contains any element of another list in Dart

Using list1.indexWhere(list2.contains) should be fine for small lists, but for large lists, the asymptotic runtime complexity would be O(m * n) where m and n are the sizes of the lists.

A different way to pose the problem of checking if a list contains any element of another list is to check if the set-intersection of two lists is non-empty. The direct way to implement that would be:

var contains = list1.toSet().intersection(list2.toSet()).isNotEmpty;

Since the default Set implementation is a LinkedHashSet, lookups would be O(1), and computing the intersection would be linear with respect to one of the Sets. However, converting each List to a Set would take linear time, making the whole operation take O(m + n).

That's asymptotically efficient, but it computes the entire intersection just to determine whether it's empty or not, which is wasteful. You can do a bit better by using .any to stop earlier and noting that .any doesn't benefit from the receiving object being a Set:

var set2 = list2.toSet();
var contains = list1.any(set2.contains);

Note that if you can use Sets in the first place instead of Lists, then the conversion cost would disappear and make the operation O(m).

Check if string in list contain all the elements from another list

Try this:

list1 = ['banana.two', 'apple.three', 'raspberry.six']
list2 = ['two', 'three']


def check(strings, substrings):
for substring in substrings:
if not (any(substring in string for string in strings)):
return False
return True


print(check(list1, list2))

How can I check if an ArrayList contains any element of another ArrayList?

Although not highly efficient, this is terse and employs the API:

if (!new HashSet<T>(list1).retainAll(list2).isEmpty())
// at least one element is shared


Related Topics



Leave a reply



Submit