As we know that Array.prototype.some() and Array.prototype.includes() has time complexity of o(n). Now I want to know that what if I will use include inside some method. The time complexity will be linear or quadratic?
function checkDublicate (arr1, arr2) {
return arr1.some(item => arr2.includes(item));
}
CodePudding user response:
It's O(mn), where m is arr1.length
and n is arr2.length
.
CodePudding user response:
Considering the worst case scenario, if arr2
has no items from arr1
, all n
elements will be searched in arr2
, with each search having O(n)
complexity. Overall complexity will be O(n^2)
(assuming n
elements in either array).