1

I have two arrays all with unique values and I want to compare them so that I get a list of all the values that consecutive

const array1 = [1a,4a,3h,78h,5b,6b,7h]
const array2 = [3h,1a,4a,5b,6b,7h]

In this case I want to compare any number of consecutively matching values I want a list of all the pairs or any number of consecutively matching unique values they have which then gives me a list like this

const array3 =[[1a,4a],[5b,6b,7h]] 

what would be the best way to go about this?

4
  • Can you explain why the expected result is [[1a,4a],[5b,6b]]? Commented Sep 7, 2021 at 14:51
  • because they are consecutive so in both the arrays 1a and 4a are followed by each other and in the second one 5b and 6b are followed by eachother so they create their own array too if that makes any more sense a Commented Sep 7, 2021 at 15:07
  • Will they always be pairs? what if [5b,6b,7b]? is this [5b, 6b], [6b, 7b] or ... Commented Sep 7, 2021 at 15:09
  • no it can be any number as long as they are consecutive so it could from as small as a pair to as big as both arrays could manage. Commented Sep 7, 2021 at 15:12

2 Answers 2

1

The only solution I can think of is (there should many better), first to create all the possible subarrays: eg: const array1 = [1a,4a,3h,78h,5b,6b,7h] const array2 = [3h,1a,4a,5b,6b,7h]

const subArray = [[1a,4a],[3h], [5b,6b,7h]]

once you have the subarray, you can map them to a new array with their Max location in either array with respect to subarray:

const maxLocation = [[1,2][2][4,5,6]]; Now you need to find the longest in sequence => [[1,2][4,5,6]]

Sign up to request clarification or add additional context in comments.

2 Comments

hey @vaira his solution does works for this example arrays given but not for all examples for example const arr1 = ['1a', '4a', '3h', '78h', '5b', '6b', '7h'] const arr2 = ['1a', '5b', '6b', '7h'] would output [ [ '1a' ], [ '5b', '6b', '7h' ] ]
I am not sure about the new answer, since I don't have enough time in hand to test it out myself
0
fetchValues = (array1, array2) => {
let result = [[]];

let loop = (i, j) => {
    if (i >= array1.length || j >= array2.length) {
        return;
    }

    if (array1[i] == array2[j]) {
        
        result[result.length - 1].push(array1[i]);
        loop(++i, ++j);
        return;
    }

    let nextIndex1 = array1.indexOf(array2[j], i);
    let nextIndex2 = array2.indexOf(array1[i], j);


    nextIndex1 = nextIndex1 < 0 ? Infinity : nextIndex1;
    nextIndex2 = nextIndex2 < 0 ? Infinity : nextIndex2;


    if (nextIndex1 !== Infinity || nextIndex2 !== Infinity) {

        result[result.length - 1].length === 0 || result.push([]);
    
        nextIndex1 > nextIndex2 ? loop(i, nextIndex2) : loop(nextIndex1, j)
    }

}
loop(0, 0);
const finalresult= result.filter(array=>array.length>1)
return finalresult;
}

This is the way I got it work for me if any one has another way then please feel free to share

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.