how to check two arrays are permutations of each other java collections code example

Example 1: javascript get intersection of two arrays

function getArraysIntersection(a1,a2){
    return  a1.filter(function(n) { return a2.indexOf(n) !== -1;});
}
var colors1 = ["red","blue","green"];
var colors2 = ["red","yellow","blue"];
var intersectingColors=getArraysIntersection(colors1, colors2); //["red", "blue"]

Example 2: create a function that checks the values of the indexes in two arrays and keep a score

const triplets = (arr1,arr2) => {
  let score1 = 0;
  let score2 = 0;
  let resultArr = [0,0]
  for (let i = 0; i < arr1.length; i++){
    if(arr1[i] === arr2[i]) {
      resultArr[0] = score1
      resultArr[1] = score2
    } else if (arr1[i] > arr2[i]) {
      score1++
      resultArr[0] = score1
    } else if (arr1[i] < arr2[i]) {
      score2++
      resultArr[1] = score2
    }
  }
  return resultArr
}

Tags:

Sql Example