I just started learning Big O notation and I'm trying to understand the Big O of different functions to see which one is better.
I'm struggling to calculate the time and space complexity for the following code.
function findCommonElem(arr1, arr2) {
let result = arr1.filter(x => arr2.includes(x));
console.log(result);
}
findCommonElem(arr1, arr2);
From what I understand, common array methods like filter() usually have a big O of O(n) so in this case, it'd be O(m+n) depending on the length of each array. However, I could be super wrong.
Can someone explain, please? Thanks so much!
Bonus question: Compared to sorting the arrays then using a while loop for the same function, which one would be considered as "better"?