How to compare two vectors in java script

Asked

Viewed 728 times

0

I have a paper to do on a subject about mathematics so I have to do about sets so I thought I’d make an intersection that would show only the numbers that repeat in both sets, but I can’t get it, I was only able to collect the data and show the typed sets.

var elemento = [];
var conjuntos = [];

numcon = prompt("Digite o Numero de Conjuntos:", "")
if (numcon == 0) {
  alert("Numero de Conjunto tem que ser maior que zero")
} else {

  for (var i = 1; i <= numcon; i++) {
    numelemt = prompt("digite o numero de elmentos do " + "  " + i + "º" + "  conjuntos:", "")
    conjuntos[i - 1] = [];
    if (numelemt == 0) {
      alert("Este conjunto sera nulo")
    } else {
      for (var cont1 = 1; cont1 <= numelemt; cont1++) {
        elemt = window.prompt("Digite o" + " " + cont1 + "º" + " " + "elemento do conjunto nº" + " " + i);

        conjuntos[i - 1].push(elemt)
      }
    }
  }
}

console.log(conjuntos)

for (var cont3 = 0; cont3 < conjuntos.length; cont3++) {
  document.write("<br>" + "conjunto " + (cont3 + 1) + " é {")
  for (var cont4 = 0; cont4 < conjuntos[cont3].length; cont4++) {
    if (cont4 == conjuntos[cont3].length - 1) {
      document.write(conjuntos[cont3][cont4]);
    } else {
      document.write(conjuntos[cont3][cont4] + ",");
    }
    //conjunto 1 é: {552655}
  }
  document.write("}");
}

  • You can give an example of that input you want to compare?

  • by ex I am typing the numbers of two sets the first 1,2,3,4, and the second 3,4,5,6, hence I wanted to compare these two vectors to show me the equal numbers in the two , that would be the 3,4

1 answer

2

It is possible to do this in a very simple way! The Array in Javascript has a method called filter() and a method called indexOf().

filter()

The filter() returns an array with the elements that apply to a condition you provide through a function.

indexOf()

The indexOf() returns the index of an element in the array. If it is not found, it is returned -1.

Solution

The solution to your problem would be:

conjuntos[i-1].filter(valor => -1 !== conjuntos[i].indexOf(valor));

See the snippet below:

var elemento = [];
var conjuntos = [];

numcon = prompt("Digite o Numero de Conjuntos:", "")
if (numcon == 0) {
  alert("Numero de Conjunto tem que ser maior que zero")
} else {

  for (var i = 1; i <= numcon; i++) {
    numelemt = prompt("digite o numero de elmentos do " + "  " + i + "º" + "  conjuntos:", "")
    conjuntos[i - 1] = [];
    if (numelemt == 0) {
      alert("Este conjunto sera nulo")
    } else {
      for (var cont1 = 1; cont1 <= numelemt; cont1++) {
        elemt = window.prompt("Digite o" + " " + cont1 + "º" + " " + "elemento do conjunto nº" + " " + i);
        conjuntos[i - 1].push(elemt)
      }
    }
  }
}

console.log(conjuntos)
for( let i = 1; i < conjuntos.length; i++) {
    const intersecao = conjuntos[i-1].filter(valor => -1 !== conjuntos[i].indexOf(valor));
    console.log(`A interseção entre os conjuntos ${i} e ${i+1} é:`, intersecao);

}

Response reference: https://stackoverflow.com/a/1885569/9036322

Browser other questions tagged

You are not signed in. Login or sign up in order to post.