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);
}
9
  • 3
    Why [8, 1]? Also, what do you mean by it is not working Commented May 25, 2015 at 7:13
  • 1,8 and 3 are there in both the arrays Commented May 25, 2015 at 7:15
  • In arr1 it has 8,1.And in arr2 also it has 8,1 as a pattern. I want to get only that two elements into arr3 Commented May 25, 2015 at 7:17
  • you are trying to get common values in arr3? Commented May 25, 2015 at 7:21
  • this might be helpful stackoverflow.com/questions/1885557/… Commented May 25, 2015 at 7:26

5 Answers 5

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
Sign up to request clarification or add additional context in comments.

2 Comments

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?
OK, I edited the answer - now it checks if i is always greater by 1 than the previous i.
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);
                }

2 Comments

No. this is not I want.
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].
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

1 Comment

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.
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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.