JavaScript JS compare the order of SOME elements in two different arrays

Both of these functions will do this comparison with a O(n) runtime, where Ori Drori's solution runs in O(n^2)

var a = [1,2,3];
var b = [0,1,4,3,9,10,2,5,6]; // 1,2,3 in wrong order
var c = [0,4,1,5,6,2,8,3,5];  // 1,2,3 in right order

// using foreach
function compare(a,b){
  var i = 0;
  b.forEach(function(el){
    if(el == a[i]) i++;
  })
  return i == a.length;
}

// using reduce
function compare2(a,b){
  return b.reduce(function(i, el){
    return el == a[i] ? i + 1 : i;
  }, 0) == a.length;
}

console.log(compare(a,b)  ==  false);  // should be false
console.log(compare(a,c)  ==  true);   // should be true
console.log(compare2(a,b) ==  false);  // should be false
console.log(compare2(a,c) ==  true);   // should be true
Answer:1

Remove all letters from the 2nd array that don't appear in 1st array using Array#filter and Array#indexOf. Then iterate the result with Array#every, and check if every character appears in the same place in the 1st array:

function compare(a, b) { 
  var arr = b.filter(function(c) { 
    return a.indexOf(c) !== -1; // use a hash object instead of indexOf if the arrays are large
  });
  
  return arr.every(function(c, i) {
    return c === a[i];
  });
}

console.log(compare(['f','t','r','m'], ['s','f','t','r','q','p','m'])); //should return true

console.log(compare(['f','t','r','m'], ['f','a','t','m','r'])); //should return false
Answer:2

You could take an index for array2 and check while iterating and return the comparison of the index and array2 and the element of array1.

function compare(array1, array2) {
    var i = 0;
    return array1.every(function (a) {
        while (i < array2.length && a !== array2[i]) {
            i++;
        }
        return a === array2[i++];
    });
}

console.log(compare(['f', 't', 'r', 'm'], ['s', 'f', 't', 'r', 'q', 'p', 'm'])); // true
console.log(compare(['f', 't', 'r', 'm'], ['f', 'a', 't', , 'm', 'r']));         // false
Answer:3

Read me: --> Im having troubles when I hover the flip card, if you move the cursor while hovering inside the div is triggering continuously and it makes an annoying effect, so here is what I need: 1) ...

Read me: --> Im having troubles when I hover the flip card, if you move the cursor while hovering inside the div is triggering continuously and it makes an annoying effect, so here is what I need: 1) ...

  1. issue with visual voicemail

I have a function that is inside a factory API.doRetrieveResults = function(name, id, values, row) { // alert(angular.toJson(values)); var getdata = { 'operation': 'RetrieveResults', '...

I have a function that is inside a factory API.doRetrieveResults = function(name, id, values, row) { // alert(angular.toJson(values)); var getdata = { 'operation': 'RetrieveResults', '...

I want to code to calculate the total number of adult, child and baby passengers. I tried with jquery but did not count the total number of all the passengers. Thanks in advance https://jsfiddle.net/...

I want to code to calculate the total number of adult, child and baby passengers. I tried with jquery but did not count the total number of all the passengers. Thanks in advance https://jsfiddle.net/...

  1. calculate total number of combinations
  2. calculate total number of days
  3. calculate total number of allowances
  4. calculate total number of hours
  5. calculate total number of possible combinations
  6. calculate total number of permutations
  7. calculate total number of hours in excel
  8. calculate total number of days in excel
  9. calculate total number from percentage
  10. calculate total number of outcomes
  11. calculate total number of atoms
  12. calculate total number of rows in excel
  13. calculate total number of subnets
  14. calculate total number of microstates
  15. calculate total number of options
  16. calculate total number of ions in a compound
  17. calculate total number of cells in excel
  18. calculate total number of coalitions
  19. calculate total number in excel
  20. calculate total number of stereoisomers

I have an array as such: [[1,3],[2,5],[1,3],[2,5]] and i would like to remove any duplicate sub-arrays. I tried using this code: uniqueArray = array.filter(function(item, pos) { return array....

I have an array as such: [[1,3],[2,5],[1,3],[2,5]] and i would like to remove any duplicate sub-arrays. I tried using this code: uniqueArray = array.filter(function(item, pos) { return array....

  1. remove duplicate arrays javascript
  2. remove duplicate arrays
  3. remove duplicate arrays python
  4. remove duplicate arrays php
  5. removing duplicate array in java
  6. removing duplicate array elements
  7. remove duplicate array js
  8. remove duplicate array object javascript
  9. remove duplicate array elements javascript
  10. remove duplicate array values php
  11. remove duplicate array swift
  12. remove duplicate array in c
  13. remove duplicate array values javascript
  14. remove duplicate array vba
  15. remove duplicate array typescript
  16. remove duplicate array jquery
  17. remove duplicate array elements in java
  18. remove duplicate array lodash
  19. remove duplicate array ruby
  20. remove duplicate array javascript es6