Thomson Reuters Interview Question: find common numbers in 2 arra... | Glassdoor

Interview Question

Senior Software Engineer Interview New York, NY

find common numbers in 2 arrays of integer, try to use O(N).

Tags:
o(n)
Answer

Interview Answer

3 Answers

0

Use HashMap

Anonymous on Mar 13, 2013
0

you could use set_intersection in STL to find the common numbers, but the arrays should be sorted, this would give O(m+n).

Shebu Appukuttan on Apr 1, 2014
0

function findCommon(arr1, arr2) {
        var seen = {};
        var common = [];
        for ( var i =0; i < arr1.length; i++) {
                seen[arr1[i]] = 1;
        }

        for ( var i =0; i < arr2.length; i++) {
                if (seen[arr2[i]] === 1) {
                        common.push(arr2[i]);
                        seen[arr2[i]]++;
                }
        }
       return common;
}

anon on Jul 6, 2015

Add Answers or Comments

To comment on this, Sign In or Sign Up.