Count exact occurrences of number in array and return True or False
I believe I understand what you want to accomplish. You want to see if the number of occurrences in the second array matches the first. If that's the case, I've used this answer as a basis


function allElementsPresent(first, second, matchAll = false) {
  if (first.length > 0 && second.length === 0) return false;
  var counts1st = {};
  var counts2nd = {};

  for (var num of first) {
    counts1st[num] = counts1st[num] ? counts1st[num] + 1 : 1;
  }
  for (var num of second) {
    counts2nd[num] = counts2nd[num] ? counts2nd[num] + 1 : 1;
  }

  for (var count in counts2nd) {
    if (matchAll && (!counts1st[count] || counts1st[count] !== counts2nd[count])) return false;
    if (!matchAll && (counts1st[count] && counts1st[count] === counts2nd[count])) return true;
  }
  return matchAll ? true : false;
}




I have two arrays
Arr1 = [1,1,1,2,2,2,3,3]  and Arr2 =[1,1,2,1]
Comparing both arrays should return True as there are same occurrences of no. 1.
However If Arr2 = [1,1,2] it should return false as the no. Of occurrences of 1 or 2 don't match with the no. Of occurrences of 1 and 2 in Arr1
Even Arr2 = [1,1,2,3,1] should return True.
Thanks in advance! Cheers
I tried this but doesn't work for other instances.
function allElementsPresent(first, second) {
  return second.every((element) => first.includes(element));
}

