(Pure js) function that Filtre array so that it returns single arrays with distinct elements

Asked

Viewed 127 times

1

var list =[
[3,4,7,9,4],[3,4,7,9,5],[3,4,7,9,6],[3,4,7,9,7],
[3,4,7,9,8],[3,4,7,9,9],[3,4,8,3,3],[3,4,8,3,4],
[3,4,8,3,5],[3,4,8,3,6],[3,4,8,3,7],[3,4,8,3,8],
[3,4,8,3,9],[3,6,4,6,7],[3,6,4,6,8],[3,6,4,6,9],
[3,6,4,7,3],[3,6,4,7,4],[3,6,4,7,5],[3,6,4,7,6],
[3,6,4,7,7],[3,6,4,7,8],[3,6,4,7,9],[3,6,4,8,3],
[3,6,4,8,4],[3,6,4,8,5],[3,6,4,8,6],[3,6,4,8,7],
[6,7,8,8,7],[6,7,8,8,8],[6,7,8,8,9],[6,7,8,9,3],
[6,7,8,9,4],[6,7,8,9,5],[6,7,8,9,6],[6,7,8,9,7],
[6,7,8,9,8],[6,7,8,9,9],[6,7,9,3,3],[6,7,9,3,4],
[6,7,9,3,5],[6,7,9,3,6],[6,7,9,3,7],[6,7,9,3,8],
[6,7,9,4,5],[6,7,9,4,6],[6,7,9,4,7],[6,7,9,4,8],
[6,7,9,4,9],[6,7,9,5,3],[6,7,9,5,4],[6,7,9,5,5],
[6,7,9,5,6],[6,7,9,5,7],[6,7,9,5,8],[6,7,9,5,9],
]

Ex: the arrays below would all be eliminated because they have repeated elements inside

[3,4,7,9,4]
[3,6,4,7,7]
[6,7,9,4,9]
[6,7,9,5,6]

In the cases below the elements are distinct but they repeat with different positioning in another array, in which case one would be deleted and the other would remain unique within the main array

[3,6,4,7,9]
[3,4,7,9,6]

[3,6,4,8,7]
[3,6,4,7,8]

3 answers

1


I think that solves your problem:

var list =[
[3,4,7,9,4],[3,4,7,9,5],[3,4,7,9,6],[3,4,7,9,7],
[3,4,7,9,8],[3,4,7,9,9],[3,4,8,3,3],[3,4,8,3,4],
[3,4,8,3,5],[3,4,8,3,6],[3,4,8,3,7],[3,4,8,3,8],
[3,4,8,3,9],[3,6,4,6,7],[3,6,4,6,8],[3,6,4,6,9],
[3,6,4,7,3],[3,6,4,7,4],[3,6,4,7,5],[3,6,4,7,6],
[3,6,4,7,7],[3,6,4,7,8],[3,6,4,7,9],[3,6,4,8,3],
[3,6,4,8,4],[3,6,4,8,5],[3,6,4,8,6],[3,6,4,8,7],
[6,7,8,8,7],[6,7,8,8,8],[6,7,8,8,9],[6,7,8,9,3],
[6,7,8,9,4],[6,7,8,9,5],[6,7,8,9,6],[6,7,8,9,7],
[6,7,8,9,8],[6,7,8,9,9],[6,7,9,3,3],[6,7,9,3,4],
[6,7,9,3,5],[6,7,9,3,6],[6,7,9,3,7],[6,7,9,3,8],
[6,7,9,4,5],[6,7,9,4,6],[6,7,9,4,7],[6,7,9,4,8],
[6,7,9,4,9],[6,7,9,5,3],[6,7,9,5,4],[6,7,9,5,5],
[6,7,9,5,6],[6,7,9,5,7],[6,7,9,5,8],[6,7,9,5,9],
]

// Eliminando arrays com elementos repetidos
let withoutDuplicates = list.filter(l => (new Set(l)).size === l.length)

// Função diff (traz a diferença entre 2 arrays)
Array.prototype.diff = function (a) {
  return this.filter((i) => a.indexOf(i) === -1)
}

let duplicatedKeys = []

// Percorrendo os arrays restantes
withoutDuplicates.forEach((a, i) => {
  // Fazendo outro loop para percorrer os arrays restantes novamente
  withoutDuplicates.forEach((b, x) => {
    // Checando se os elementos comparados não são o mesmo elemento
    // e checando se a diferença entre eles é igual a 0 (se for significa
    // que eles possuem os mesmo valores, mesmo em posições diferentes)
    if(i !== x && a.diff(b).length === 0) {
        // Verifica se o elemento a e b não foram incluídos no array duplicatedKeys
        if(duplicatedKeys.indexOf(a) === -1 && duplicatedKeys.indexOf(b) === -1) {
            // Armazena o elemento no array duplicatedKeys
            duplicatedKeys.push(a)
        }
    }
  })
})

// Removendo as chaves duplicadas que foram armazenadas no array duplicatedKeys
withoutDuplicates = withoutDuplicates.filter(a => duplicatedKeys.indexOf(a) === -1)

console.log(withoutDuplicates)

When I get home edit and try to explain better what was done...

  • not working properly,[ 6,7, 9, 5, 8 ] [ 6,7,8,9, 5 ] that two for example would have to stay only 1 of them

  • @Lucasmota I think it’s working now, take a look

  • 1

    Not yet. Ex: these two array are equal [[6,7,8,9,5], [6,7,9,5,8]], have to stay at least one of them to be unique within the main array, here on this site there is a brief explanation in example 2 where it talks about combinations

  • @Lucasmota I think now yes it worked, take a look!

  • 1

    the site was here https://brasilescola.uol.com.br/matematica/arrangementou-combinacao.htm . Now it’s working perfectly, thanks for the help and the attention.

  • @Lucasmota I edited the comments, I think it will be a little better to understand now!

Show 1 more comment

0

var lista = [
  [3,4,7,9,4],[3,4,7,9,5],[3,4,7,9,6],[3,4,7,9,7],
  [3,4,7,9,8],[3,4,7,9,9],[3,4,8,3,3],[3,4,8,3,4],
  [3,4,8,3,5],[3,4,8,3,6],[3,4,8,3,7],[3,4,8,3,8],
  [3,4,8,3,9],[3,6,4,6,7],[3,6,4,6,8],[3,6,4,6,9],
  [3,6,4,7,3],[3,6,4,7,4],[3,6,4,7,5],[3,6,4,7,6],
  [3,6,4,7,7],[3,6,4,7,8],[3,6,4,7,9],[3,6,4,8,3],
  [3,6,4,8,4],[3,6,4,8,5],[3,6,4,8,6],[3,6,4,8,7],
  [6,7,8,8,7],[6,7,8,8,8],[6,7,8,8,9],[6,7,8,9,3],
  [6,7,8,9,4],[6,7,8,9,5],[6,7,8,9,6],[6,7,8,9,7],
  [6,7,8,9,8],[6,7,8,9,9],[6,7,9,3,3],[6,7,9,3,4],
  [6,7,9,3,5],[6,7,9,3,6],[6,7,9,3,7],[6,7,9,3,8],
  [6,7,9,4,5],[6,7,9,4,6],[6,7,9,4,7],[6,7,9,4,8],
  [6,7,9,4,9],[6,7,9,5,3],[6,7,9,5,4],[6,7,9,5,5],
  [6,7,9,5,6],[6,7,9,5,7],[6,7,9,5,8],[6,7,9,5,9],
]

// 1º Passo - Remover os arrays com itens repetidos.
lista = lista.filter(function (sublista) {
  return sublista.every(function (item, indice) {
    return sublista.indexOf(item) == indice
  })
})

// 2º Passo - não é possivel comparar dois Objetos por valor no JavaScript.
// desta forma, [1, 2, 3] != [1, 2, 3]. porém como se trata de um array de numeros,
// é possivel converter o mesmo para uma string de base64 com a função btoa
// para que que a string base64 para arrays com os mesmos numeros seja igual,
// devemos ordenar os arrays com a função sort, antes de obter a string base64.
// por exemplo, [3, 4, 5, 7, 9] será convertido para "Myw0LDUsNiw4"
var indices = {}
var binarios = lista.map(function (sublista, indice) {
  return { indice: indice, binary: btoa(sublista.sort()) } 
})

// 3º Passo - obtendo o indice da primeira ocorrencia de cada array,
// assim como a quantidade de ocorrencias de arrays com os mesmo elementos.
binarios.forEach(function (item, indice) {
  if (indices[item.binary]) {
    indices[item.binary].total++
  } else {
    indices[item.binary] = { indice: indice, total: 1 }
  }
})

// 4º Passo - Filtrar os arrays, para exibir apenas os arrays cujo a conjunto numerico é unico.
lista = Object.values(indices).filter(function (item) {
  return item.total == 1
}).map(function (item) {
  return lista[item.indice]
})

console.log(lista)

  • Not working properly. Sample : The array [6,7,9,3,5] was not included.

0

When doing the table test, results in the following arrays:

[3,4,7,9,5]
[3,4,7,9,8]
[3,6,4,7,5]
[3,6,4,8,5]
[3,6,4,8,7]
[6,7,9,3,4]
[6,7,9,3,8]
[6,7,9,4,8]
[6,7,9,5,3]
[6,7,9,5,4]
[6,7,9,5,8]

The code below filters the way you want, replacing the original array list with the filtered elements:

var list =[
[3,4,7,9,4],[3,4,7,9,5],[3,4,7,9,6],[3,4,7,9,7],
[3,4,7,9,8],[3,4,7,9,9],[3,4,8,3,3],[3,4,8,3,4],
[3,4,8,3,5],[3,4,8,3,6],[3,4,8,3,7],[3,4,8,3,8],
[3,4,8,3,9],[3,6,4,6,7],[3,6,4,6,8],[3,6,4,6,9],
[3,6,4,7,3],[3,6,4,7,4],[3,6,4,7,5],[3,6,4,7,6],
[3,6,4,7,7],[3,6,4,7,8],[3,6,4,7,9],[3,6,4,8,3],
[3,6,4,8,4],[3,6,4,8,5],[3,6,4,8,6],[3,6,4,8,7],
[6,7,8,8,7],[6,7,8,8,8],[6,7,8,8,9],[6,7,8,9,3],
[6,7,8,9,4],[6,7,8,9,5],[6,7,8,9,6],[6,7,8,9,7],
[6,7,8,9,8],[6,7,8,9,9],[6,7,9,3,3],[6,7,9,3,4],
[6,7,9,3,5],[6,7,9,3,6],[6,7,9,3,7],[6,7,9,3,8],
[6,7,9,4,5],[6,7,9,4,6],[6,7,9,4,7],[6,7,9,4,8],
[6,7,9,4,9],[6,7,9,5,3],[6,7,9,5,4],[6,7,9,5,5],
[6,7,9,5,6],[6,7,9,5,7],[6,7,9,5,8],[6,7,9,5,9],
];

var list2 = list.map(function(v){ return v.join('') });

for(var item in list2){
   list2[item] = list2[item].split('').sort().join('')+" "+item;
}

for(var item in list2){
   var m = list2[item].match(/^\d+/)[0];
   for(var x=0; x<m.length; x++){
      var r = m.match( new RegExp(m[x], 'g') ).length;
      if(r > 1){
         list2[item] = list2[item].replace(m+' ','');
         break;
      }
   }
}

var list2_str = list2.join(); // converto para string

for(var item of list2){
   var padrao = item.match(/^\d+\s/);
   var m = list2_str.match( new RegExp(padrao, 'g') );
   if(m){
      for(var x=1; x<m.length; x++){
         list2_str = list2_str.replace(m[x], '');
      }
   }
}

list2_str = list2_str.split(','); // converto para array

list2_str.filter(function(v){
   if(v.length < 7) list[v] = null;
});

list = list.filter(function(v){ return v; });

// daqui pra baixo apenas para mostrar na div
var div = document.querySelector("#resultado");

for(var item in list){
    div.innerHTML += item+": ["+list[item]+"]<br>";
}
<div id="resultado"></div>

  • this one is working too! thanks so much for the help

Browser other questions tagged

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