I'm looking for a function that returns true if and only if a given array includes all the elements of another target array which may include two or more occurrences of the same element.
Sample data:
const target = [ 1, 3, 3 ];
const array1 = [ 1, 2, 3, 4 ]; // false
const array2 = [ 1, 3, 3, 4 ]; // true
const array3 = [ 1, 3, 4, 3 ]; // true
const array4 = [ 3, 2, 1, 3 ]; // true
const array5 = [ 1, 1, 3, 3 ]; // true
This question's answer is close but doesn't account for duplicates in target array.
This question's answer works for Ruby but I'm looking for a Javascript solution.
When not accounting for duplicates, .indexOf() and .includes() make it rather easy and there are elegant one-liner solutions. But I'm unsure how to keep track of duplicates and suspect a more iterative approach is needed.
target, remove that element fromtarget. At the end, check iftargetis empty.[ 1, 1, 3, 3]? True or false?