Determine if one array contains all elements of another array, including any duplicates
const isMultiSubset = (target, value) => {
const occurences = new Map;
for(const entry of target)
occurences.set(entry, (occurences.get(entry) ?? 0) + 1);
for(const entry of value)
if (occurences.has(entry))
occurences.set(entry, occurences.get(entry) - 1);
return [...occurences.values()].every(count => count <= 0);
};
By using a Map to count occurences this can be solved in O(n + m).
Check to see if an array contains all elements of another array, including whether duplicates appear twice
Try creating this function:
function containsAll (target, toTest) {
const dictionary = {}
target.forEach(element => {
if (dictionary[element] === undefined) {
dictionary[element] = 1;
return;
}
dictionary[element]++;
});
toTest.forEach(element => {
if (dictionary[element] !== undefined)
dictionary[element]--;
})
for (let key in dictionary) {
if (dictionary[key] > 0) return false;
}
return true;
}
Then invoke it like this:
const arr1 = [1, 2, 2, 3, 5, 5, 6, 6]
const arr2 = [1, 2, 3, 5, 6, 7]
console.log(containsAll(arr1, arr2)) // returns false
How can I check it an array contains all elements from another array, including count, in JavaScript?
You can try to loop through second array and compare t against main array if value found make main array cell to false and set flag as true
arr1 = [1, 2, 3, 1, 2, 3, 4]arr2 = [1, 3, 1, 1]arr3 = [1, 1, 2, 2, 3]
function checkArray(compareMe, toThis){ var flag = false; for(var i = 0; i < toThis.length; i++){ flag = false; for(var j = 0; j < compareMe.length; j++){ if(compareMe[j] == toThis[i]){ compareMe[j] = false; flag = true; break; } } } return flag;}
console.log(checkArray(arr1, arr2));console.log(checkArray(arr1, arr3));
PHP: Check if an array contains all array values from another array
Look at array_intersect().
$containsSearch = count(array_intersect($search_this, $all)) === count($search_this);
Or for associative array, look at array_intersect_assoc().
Or for recursive compare of sub-arrays, try:
<?php
namespace App\helpers;
class Common {
/**
* Recursively checks whether $actual parameter includes $expected.
*
* @param array|mixed $expected Expected value pattern.
* @param array|mixed $actual Real value.
* @return bool
*/
public static function intersectsDeep(&$expected, &$actual): bool {
if (is_array($expected) && is_array($actual)) {
foreach ($expected as $key => $value) {
if (!static::intersectsDeep($value, $actual[$key])) {
return false;
}
}
return true;
} elseif (is_array($expected) || is_array($actual)) {
return false;
}
return (string) $expected == (string) $actual;
}
}
check if an javascript array contains all of the values of another array
Your code does not work, because it always returns anything (if not false, then true) in first iteration (reason: return true statement is in for loop). Try this:
let arrayContainsArray = (a, b) => { let a_array = a.split(" ") let b_array = b.split(" ")
for (let i = 0; i < b_array.length; i++) { if (a_array.includes(b_array[i])) { let index = a_array.indexOf(b_array[i]) a_array.splice(index, 1) } else { return false } } return true}
console.log(arrayContainsArray('two times three is not four', 'two times two is four'));console.log(arrayContainsArray('A B C D', 'B B C D'));console.log(arrayContainsArray('apple banana orange mango', 'banana orange'));
Check if an array contains 2 or more elements of another array in JavaScript
Firstly, you can use array#filter
combined with array#includes
to find all items in arr1 in arr2.
Then check the length of result.
let arr1 = [1, 2, 3];
let arr2 = [2, 3];
let result = arr1.filter(v1 => arr2.includes(v1));
console.log(result.length >= 2);
Related Topics
How to Call Shell Commands from Ruby
Get the Name of the Currently Executing Method
Converting a Nested Hash into a Flat Hash
Rhc Setup Gives Error 'No Such File Dl/Import'
Difference Between Ruby 1.8 and Ruby 1.9
Difference Between Datetime and Time in Ruby
Add a Default Value to a Column Through a Migration
What's the Difference Between Equal, Eql, ===, and ==
Do..End VS Curly Braces For Blocks in Ruby
How to Get Source Code of a Method Dynamically and Also Which File Is This Method Locate In
Difference Between Collection Route and Member Route in Ruby on Rails
Why Does Installing Nokogiri on MAC Os Fail With Libiconv Is Missing