-6

I have two arrays as below:

var arr1 = [1,2,3,8,7,5,7,2,9,0];
var arr2 = [8,7,5];

I want to compare arr2 with arr1, when it will find arr2 exactly in the same sequence as it is then it should return true. i.e. if [8,7,5] is found exactly same sequence in arr1 then it will return true.

Note: We have to do this without using indexOf.

gyre
  • 16,369
  • 3
  • 37
  • 47
Kishan
  • 350
  • 2
  • 10
  • 31
  • 5
    Have you tried anything so far? Do you know what a loop is and how to use it to iterate over an array? – Felix Kling Apr 25 '17 at 15:27
  • 1
    Without code it looks like you didn't try at all? – Niall Maher Apr 25 '17 at 15:28
  • 2
    @Dez: That doesn't take the order/sequence into account. – Felix Kling Apr 25 '17 at 15:28
  • 3
    @Dez I don't think this is quite a duplicate, because this question asks about a subsequence, while that question asks about a subset. – apsillers Apr 25 '17 at 15:28
  • This will answer your question (using difference/intersection): http://stackoverflow.com/questions/14130104/how-do-i-test-if-one-array-is-a-subset-of-another – Nicholas Apr 25 '17 at 15:30
  • 1
    `arr1.join().includes(arr2.join())` – baao Apr 25 '17 at 15:30
  • @Nicholas: If they can't use `.indexOf()`, they probably can't use a library. And that's a different task anyway. The OP isn't looking merely for an intersection. –  Apr 25 '17 at 15:31
  • @baao: `.includes()` is basically `.indexOf()`. –  Apr 25 '17 at 15:32
  • 4
    Showing what you've done so far helps demonstrate how much you currently understand and don't understand, which allows you to get an answer tailored to your current level of knowledge (and prevents endless back-and-forth of "well, I don't understand that answer, can you explain this more?" and "yes, I already knew all of that, but you skipped right over X which is what I really didn't understand..." etc) – apsillers Apr 25 '17 at 15:32
  • @squint :D only basically :-) – baao Apr 25 '17 at 15:33
  • 2
    I love when people ask us to do their homework. – epascarello Apr 25 '17 at 15:33
  • 1
    In short, you loop through `arr1` - you check if the current item in `arr1` matches the first item in `arr2`, if it does you check the second item in `arr2` against the next item in `arr1` and so on until you reach the end of `arr2` (in which case you return true). If any items don't match, you move to the next item in `arr1` and start again at the beginning of `arr2`. If you run out of items in `arr1`, return false. So go ahead and give it a try and come back if and when you get stuck. – Matt Burland Apr 25 '17 at 15:35
  • And with these answers, once again StackOverflow demonstrates itself to be the place that enables you to never actually have to think or learn. –  Apr 25 '17 at 15:36
  • @squint: Far from it. I don't think anybody has given the OP a complete answer they could actually use in their homework yet. – Matt Burland Apr 25 '17 at 15:38
  • 1
    @epascarello But it is good, no? So if the guy doesn't want to do his homework himself, he gives us nice opportunity to practice our skills answering, making us better in what we do, at the same time making himself worser programmer than he could be. Se we have less competition and higher salaries! More bad programmers, isn't it good!? – dfsq Apr 25 '17 at 15:38
  • @MattBurland: I don't know if this is homework or not, but I do see a complete solution below. –  Apr 25 '17 at 15:40
  • 2
    @dfsq The problem is people give them the answers, they are not learning. The way Stackoverflow is designed is not to lead people to solutions, but to give answers to problems. The issue here is person has no clue so they ask a question without showing what they attempt Some point hungry person charges in and gives a solution with zero explanation. This question is a simple looping problem. – epascarello Apr 25 '17 at 15:41

2 Answers2

1

You could use a combination of Array#some and Array#every.

var array1 = [1, 2, 3, 8, 7, 5, 7, 2, 9, 0],
    array2 = [8, 7, 5],
    result = array1.some(function (a, i, aa) {
        return array2.every(function (b, j) {
            return aa[i + j] === b;
        });
    });
    
console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

You can loop through the largest array. On each iteration, compare the next values to all of the values found in the smaller array. If they all match, then it contains the smaller array.

var arr1 = [1,2,3,8,7,5,7,2,9,0];
var arr2 = [8,7,5];

console.log(doesArrayContain(arr2, arr1));

function doesArrayContain(smallestArray, biggestArray) {
    for (var i = 0; i < biggestArray.length; i++) {
     var doesMatch = true;
        
     for (var j = 0; j < smallestArray.length; j++) {
            if (biggestArray[i + j] !== smallestArray[j]) {
             doesMatch = false; break;
            }
        }
        
        if (doesMatch) {
            return true;
        }
    }
    
    return false;
}
Toby Mellor
  • 8,093
  • 8
  • 34
  • 58