OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Javascript: Set Data Structure: intersect

  • Thread starter Thread starter Snorlax
  • Start date Start date
S

Snorlax

Guest
Trying to get two data sets to intersect but I can't do it. For example, in my code below, intersecting mySet and mySet2 should yield "1" since they both have a value of "1" in their set.

Code:
var mySet = new Set();
var mySet2=new Set();
mySet.add(1);
mySet.add(2);
mySet.add("HELLOOOOO");
mySet2.add("hi");
mySet2.add(1);


var a = Array(mySet, mySet2);
console.log(a);

mySet.forEach(function(value) {
    console.log(value);
});


mySet2.forEach(function(value) {
    console.log(value);
});

function intersection_destructive(a, b)
{
    var result = new Array();
    while( mySet.length > 0 && mySet2.length > 0 )
    {
        if      (mySet[0] < mySet2[0] ){ mySet.shift(); }
        else if (mySet[0] > mySet2[0] ){ mySet2.shift(); }
        else /* they're equal */
        {
            result.push(mySet.shift());
            mySet2.shift();
        }
    }

    return result;
}

Set 1 and Set 2 both have "1" in it but my function (intersection_destructive) doesn't return it. I'm not sure how to intersect them, I searched stackoverflow and found intersection_destructive but it didn't work for me, I also tried:

Code:
array1.filter(function(n) {
    return array2.indexOf(n) != -1
});

as per this: Simplest code for array intersection in javascript

but I get an error on filter when I try to run it.

<p>Trying to get two data sets to intersect but I can't do it. For example, in my code below, intersecting mySet and mySet2 should yield "1" since they both have a value of "1" in their set.</p>
<pre><code>var mySet = new Set();
var mySet2=new Set();
mySet.add(1);
mySet.add(2);
mySet.add("HELLOOOOO");
mySet2.add("hi");
mySet2.add(1);


var a = Array(mySet, mySet2);
console.log(a);

mySet.forEach(function(value) {
console.log(value);
});


mySet2.forEach(function(value) {
console.log(value);
});

function intersection_destructive(a, b)
{
var result = new Array();
while( mySet.length > 0 && mySet2.length > 0 )
{
if (mySet[0] < mySet2[0] ){ mySet.shift(); }
else if (mySet[0] > mySet2[0] ){ mySet2.shift(); }
else /* they're equal */
{
result.push(mySet.shift());
mySet2.shift();
}
}

return result;
}
</code></pre>
<p>Set 1 and Set 2 both have "1" in it but my function (intersection_destructive) doesn't return it. I'm not sure how to intersect them, I searched stackoverflow and found intersection_destructive but it didn't work for me, I also tried:</p>
<pre><code>array1.filter(function(n) {
return array2.indexOf(n) != -1
});
</code></pre>
<p>as per this: <a href="https://stackoverflow.com/questions/1885557/simplest-code-for-array-intersection-in-javascript">Simplest code for array intersection in javascript</a></p>
<p>but I get an error on <code>filter</code> when I try to run it.</p>
 
Top