Menu

gitpiper

isContainedIn javascript Code Snippet in 2024

arrayintermediate

Last Updated: 21 December 2024

Checks if the elements of the first array are contained in the second one regardless of order.

  • Use a for...of loop over a Set created from the first array.
  • Use Array.prototype.some() to check if all distinct values are contained in the second array.
  • Use Array.prototype.filter() to compare the number of occurrences of each distinct value in both arrays.
  • Return false if the count of any element is greater in the first array than the second one, true otherwise.
const isContainedIn = (a, b) => { for (const v of new Set(a)) { if ( !b.some(e => e === v) || a.filter(e => e === v).length > b.filter(e => e === v).length ) return false; } return true; };
isContainedIn([1, 4], [2, 4, 1]); // true

javascript snippet similar to isContainedIn For You in December 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️