检查一个数组中的每个元素是否都在第二个数组中

我有两个数组,我想检查是否所有元素arr2都在arr1。如果重复元素的值arr2,则它需要arr1具有相同的次数。这样做的最佳方法是什么?


arr1 = [1, 2, 3, 4]

arr2 = [1, 2]


checkSuperbag(arr1, arr2)

> true //both 1 and 2 are in arr1


arr1 = [1, 2, 3, 4]

arr2 = [1, 2, 5]


checkSuperbag(arr1, arr2)

> false //5 is not in arr1


arr1 = [1, 2, 3]

arr2 = [1, 2, 3, 3]


checkSuperbag(arr1, arr2)

> false //3 is not in arr1 twice


当年话下
浏览 741回答 3
3回答
打开App,查看更多内容
随时随地看视频慕课网APP