Find intersecting elements amongst arrays

First off, a small note on associative arrays in JavaSript :

JavaScript doesn’t really support associative arrays but since all arrays are objects, associative arrays can be emulated by using it as an object.

// Actual array form
var digits = new Array(0,1,2,3,4,5,6,7,8,9);
alert(digits.length); // 10
// JavaScript Object emulated as an Associative array
var numbers = {'zero':0, 'one':1, 'two':2, 'three':3, 'four':4, 'five':5, 'six':6, 'seven':7, 'eight':8, 'nine':9}
alert(numbers.length); // undefined
var c = 0; for (var n in numbers) c++; alert(c); // 10
// Now add a 'function' to the numbers 'class'
numbers.add = function(a,b) { return a + b; }
c = 0; for (var n in numbers) c++; alert(c); // 11

So when we pass an ‘associative’ array to a function, we assume that you are truly passing an associative array containing only key:value pairs and no functions/methods associated with it.

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<title>Find Intersection Among Arrays</title>
<style type="text/css">
</style>
<script type="text/javascript">
/*
ar : can be an array of arrays or an asssociative array

Examples:
================

intersect([[76,80,11,31,34,82,100], [13,31,66,43,73,100,73,10]])
will give [31,100]

intersect(
{
    colors1 : ['black', 'orange', 'white', 'blue', 'yellow'],
    colors2 : ['green', 'yellow', 'red', 'brown', 'black']
})
will give ['black', 'yellow']

intersect(
{
    colors1 : ['black', 'orange', 'white', 'blue', 'yellow'],
    colors2 : ['green', 'yellow', 'red', 'brown', 'black'],
    colors3 : ['yellow', 'red', 'pink', 'orange']
})
will give ['yellow']
*/
function intersect(ar)
{
    if (ar == null) return false;

    var a = new Array();

    if (ar.length == undefined) // Associative Array
    {
        for (var i in ar)
         a.push(ar[i]);
    }
    else
     a = ar;

    if (a.length == 1) return false; // Single array ? Nothing to intersect with

    var common = new Array();
    function loop(a, index, s_index, e_index)
    {
        if (index == null) index = 0;
        if (s_index == null) s_index = 0;
        if (e_index == null) e_index = a[index].length;
        if (index == a.length - 1) return;

        for (var i = s_index; i < e_index; i++)
        {
            if (common.indexOf(a[index][i]) != -1) continue;
            for (var j = 0; j < a[index + 1].length; j++)
            {
                if (a[index][i] != a[index+1][j]) continue;
                loop(a, index + 1, j, j + 1);
                if (index + 1 == a.length - 1) { common.push(a[index][i]); break; }
            }
        }
    }

    loop(a);
    return common;
}
</script>
</head>
<body>
<script type="text/javascript">
var a = [5, 8, 23, 6, 69];
var b = [96, 5, 7, 32, 69, 6];
var common = intersect([a,b]);
alert("Intersection of [5,8,23,6,69] and [96,5,7,32,69,6] = " + common);

var c = [85, 88, 5, 32, 23]
var common = intersect([a,b,c]);
alert("Intersection of [5,8,23,6,69] and [96,5,7,32,69,6] and [85,88,5,32,23] = " + common);

var as =
{
    colors1 : ['black', 'orange', 'white', 'blue', 'yellow'],
    colors2 : ['green', 'yellow', 'red', 'brown', 'black'],
    colors3 : ['yellow', 'red', 'pink', 'orange']
};
var common = intersect(as);
alert("Intersection of associative array as = " + common);
</script>
</body>
</html>
Posted in General | 1 Comment