Check whether an array is a subset of another
bool isSubset = !t2.Except(t1).Any();
Find whether an array is subset of another array hashtable way(Python)
In Python, you would use set
objects for this:
>>> arr1 = [11, 1, 13, 21, 3, 7]
>>> arr2 = [11, 3, 7, 1]
>>> set(arr1).issuperset(arr2)
True
Or more efficiently, use:
>>> set(arr2).issubset(arr1)
True
If you expect arr2 to be much smaller...
Some quick timings, it seems that they are about the same in rumtime, although, creating a set
from arr1
will require much more auxiliary memory:
>>> import numpy as np
>>> arr1 = np.random.randint(0, 100, (1000000,)).tolist()
>>> len(arr1)
1000000
>>> from timeit import timeit
>>> arr2 = [11, 3, 7, 1]
>>> timeit('set(arr1).issuperset(arr2)', 'from __main__ import arr1, arr2', number=1000)
14.337173405918293
>>> timeit('set(arr2).issubset(arr1)', 'from __main__ import arr1, arr2', number=1000)
14.459818648989312
Checking if an array is a subset of another array (but checking against a property)
var object1 = {name: 'one'};
var object2 = {name: 'two'};
var object3 = {name: 'three'};
var arr1 = [object1,object2,object3];
var arr2 = ['one','two'];
// solution
var names = arr1.map(function(obj) {
return obj.name;
});
var isSuperset = arr2.every(function(val) {
return names.indexOf(val) >= 0;
});
alert(isSuperset);
Test if one array is a subset of another
If you start from strings, you could check strstr($fullString,$subsetStr);
. But that'll only work when all chars have the same order: 'abcd','cd'
will work, but 'abcd','ad'
won't.
But instead of writing your own, custom, function you should know that PHP has TONS of array functions, so its neigh on impossible that there isn't a std function that can do what you need it to do. In this case, I'd suggest array_diff
:
$srcString = explode('>','string1>string2>string3>string4>string5');
$subset = explode('>','string3>string2>string5');
$isSubset = array_diff($subset,$srcString);
//if (empty($isSubset)) --> cf comments: somewhat safer branch:
if (!$isSubset)
{
echo 'Subset';
return true;
}
else
{
echo 'Nope, substrings: '.implode(', ',$isSubset).' Didn\'t match';
return false;
}
JEST - check if objects array is a subset of another array
I've never tried this myself, but wouldn't it work to just say:
expect(users).toEqual(
expect.arrayContaining(subset)
)
Check if an array is subset of another array in Ruby
Use sets. Then you can use set.subset?
. Example:
require 'set'
a1 = Set[3,6,4]
a2 = Set[1,2,3,4,5,6,7,8,9]
puts a1.subset?(a2)
Output:
true
See it working online: ideone
Related Topics
How to Monitor Clipboard Changes in C#
Entity Framework 5 Updating a Record
Pass Extra Parameters to an Event Handler
How to Convert an Integer into Its Verbal Representation
How to Rethrow an Exception in C#
Setting Authorization Header of Httpclient
SQL Data Reader - Handling Null Column Values
How to Apply an Xslt Stylesheet in C#
Odbcconnection Returning Chinese Characters as ""
Client to Send Soap Request and Receive Response
Multidimensional Array [][] VS [,]
How to Compile .Net Core App for Linux on a Windows MAChine
Using Transactions or Savechanges(False) and Acceptallchanges()