2

i have 2 arrays.

 arr1=[1,8,1,3,2]

 arr2=[3,8,1]

I want to put elements [8,1] subset into arr3. How can i do this using javascript?I used the following code. But doesn't seemed to be working.

function subsetFind() {      
    var arr1 = [1,8,1,3,2]
    var arr2 = [3,8,1]
    var arr3 = [];
    var arr1length = arr1.length;
    var arra2length = arr2.length;

   for(var i = 0; i < arr1length; ++i){
        for(var j=0;j<arra2length;j++) {
            if(arr1[i] != arr2[j]) {
                break;
            } else {
                arr3.push(arr1[i]);
                break;
            }
        }
    }

    alert(arr3);
}
AVM
  • 592
  • 2
  • 11
  • 25
Tharu
  • 353
  • 1
  • 2
  • 11

5 Answers5

1

Try this solution - it checks whether both current and previous OR current and next value is equal:

function subsetFind() {
    var arr1 = [1,8,1,3,2]
    var arr2 = [3,8,1]
    var arr3 = [];
    var arr1length = arr1.length;
    var arra2length = arr2.length;
    var used_i = 0;
    for(var i = 0; i < arr1length; ++i){
        if(used_i != 0 && used_i < i-1){
            break;
        }
        for(var j=0;j<arra2length;j++) {
            if((arr1[i] == arr2[j] && arr1[i-1] == arr2[j-1]) || (arr1[i] == arr2[j] && arr1[i+1] == arr2[j+1])) {
               arr3.push(arr1[i]);
               used_i = i;
            }
        }
    }

    alert(arr3);
}

Output:

8,1
n-dru
  • 9,285
  • 2
  • 29
  • 42
  • It gives the correct answer for this two arrays. But think for the following arrays it won't give the answer. arr1=[1,9,3,5,4,8,2,6,3,4] , arr2=[5,2,4,8,2,6,4]. For arr3 it should give only [4,8,2,6]. But it gives [4,8,2,6,4]. Can you fix that? – Tharu May 25 '15 at 07:26
  • OK, I edited the answer - now it checks if `i` is always greater by 1 than the previous `i`. – n-dru May 25 '15 at 08:15
0

I hope that you want; (renewed :)

                function subset() {
                    var arr1 = [1, 9, 3, 5, 4, 8, 2, 6, 3, 4]
                    var arr2 = [5, 2, 4, 8, 2, 6, 4]
                    var arr3 = [];

                    var minSize=2; // minimum 2 element must in intersection
                    var findedMax = "";

                    var arr1Joined = ""; arr1.forEach(function (a) { arr1Joined += "," + a; });
                    
                    for(k=minSize;k<arr2.length;k++)
                        arr2.forEach(function (x,y) {
                            var fkey="";
                            for (i = y; i <= y+k; i++)
                                fkey += "," + arr2[i];
                            if (arr1Joined.indexOf(fkey) >= 0) findedMax = fkey;
                    });
                    arr3=findedMax.substr(1).split(",");
                    alert(arr3);
                }
Serhat MERCAN
  • 1,078
  • 3
  • 14
  • 31
  • No. this is not I want. – Tharu May 25 '15 at 07:50
  • This is what I want. But can you improve the answer to get more consequent element. For an example, arr1 = [1, 9, 3, 5, 4, 8, 2, 6, 3, 4,3,7] , arr2 = [5, 2, 4, 8, 2, 6, 4,1,3,7]. Since 3,7 also inside both the arrays arr3 should contains [4,8,2,6,3,7]. – Tharu May 25 '15 at 08:44
0

Try This Out:

Reference n-dru's answer:

function subset () {
        var arr1 = [1,9,3,5,4,8,2,6,3,4]
        var arr2 = [5,2,4,8,2,6,4]
        var arr3 = [];
        var arr1length = arr1.length;
        var arra2length = arr2.length;
        var finalResult;
       for(var i = 0; i < arr1length; ++i){
            for(var j=0;j<arra2length;j++) {
                if((arr1[i] == arr2[j] && arr1[i-1] == arr2[j-1]) || (arr1[i] == arr2[j] && arr1[i+1] == arr2[j+1])) {
                    arr3.push(arr1[i]);

                }
                else
                {
                    finalResult = arr3.toString();
                }
            }
        }

        alert(finalResult);
}

DEMO

Amy
  • 4,034
  • 1
  • 20
  • 34
  • This is also not I want. This gives the answer. But if I change the 1st array to [1,9,3,5,4,8,2,6,3,4,4,8], Then also it should gives [4,8,2,6]. But it gives [4,8,2,6,4,8]. According to my requirement, [4,8,2,6,4,8] is not represented in array 2. It has only 4,8,2,6 consequently. So I need only that answer. – Tharu May 25 '15 at 07:47
0

I believe Your question was alredy answerd in: Simplest code for array intersection in javascript and Finding matches between multiple JavaScript Arrays . You can also look into implementation of _.intersection in lowdash library.

Community
  • 1
  • 1
Rafal Bartoszek
  • 101
  • 1
  • 3
0

ES6 way.

[...new Set(arr1)].filter(v => arr2.includes(v))

Break down:

new Set(arr1)                   // convert arr1 to Set to remove duplicates 

[...new Set(arr1)]              // convert back to array

arr2.includes(v)                // test if arr2 includes `v`

[...new Set(arr1)].filter(v => arr2.includes(v))  // choose unique elements in both arrays
Isaac Han
  • 348
  • 3
  • 8