Big O Difficulty Algorithm

I just learn Big O notation, and I went through a few mysterious puzzles, and I just thought I would check with people if I was thinking about things right.

In Javascript, will this be considered an O (n) time solution for finding common elements across two arrays? Or does the language search in the object and iterate through n elements inside the object in the same way that it iterates through an array?

function findCommon (input, input2){
  var key = {};
  var out = [];
  for(var i=0; i<input.length; i++){
    key[input[i]] = true;
  }
  for(var j=0; j<input2.length; j++){
    if(key[input2[j]] == true){
      out.push(input2[j]);
    }
  }
  return out;
} 

findCommon ([1, 2, 4, 6, 7], [3, 4, 5, 7]) -> [4, 7]

+4
source share
2 answers

m - input n - input2. O(m), - O(n). , , ( , ), O(m + n), , , .

m + n <= 2 * max(m, n), , O(2 * max(m, n)), , , , O(max(m, n)). , n , m ( , ). O(n), , O(n), O(m + n).

+3

O(n) . O(n). , , O(n) O(m), O(2n) ( ). , m n, ( for loop )

+2

Source: https://habr.com/ru/post/1530820/


All Articles