(pure Js) function that filters array so that it returns unique arrays with distinct elements

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 deleted 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 are repeated with different positioning in another array, in which case one would be deleted and another 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]
    
asked by anonymous 02.05.2018 / 21:02

3 answers

1

I think this 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 I edit and try to explain what was done ...

    
02.05.2018 / 21:51
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)
    
02.05.2018 / 22:25
0

When doing the table test, it 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 by 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>
    
03.05.2018 / 06:46