0

In this case I have 3 arrays in javascript:

var array1 = ['124','10','100','190','1000'];
var array2 = ['124','100','190', '45'];
var array3 = ['124','100','175','19','1900'];

I need a script that get the unique values from 3 or more arrays (in this case 3 arrays). the result should be:

['10','1000','45','175','19','1900']

Thanks for the help

3 Answers3

2

You could take a Map and set known keys to value false and vice versa.

Then map the map, filter and map only the keys.

In pieces:

  • Make a single array with all values from the arrays with spread syntax ....
  • Reduce the array wih a Map and set unseen values to true and seen values to false. The result ia a map with all values as key and as value either true or false, depending on uniqueness or not.
  • By having a map, you need to take only the keys with a value true. This requires an array from the map.
  • Filter the array to get only unique keys.
  • Map the array to get only the key.

The result is an array with unique values.

const
    a = ['124', '10', '100', '190', '1000'],
    b = ['124', '100', '190', '45'],
    c = ['124', '100', '175', '19', '1900'],
    unique = Array
        .from([...a, ...b, ...c].reduce((m, v) => m.set(v, !m.has(v)), new Map))
        .filter(([, b]) => b)
        .map(([v]) => v);

console.log(unique);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • that's an elegant setup, but not for the fainted heart beginners will struggle to understand step-by-step, maybe you could elaborate for them – balexandre May 13 '21 at 16:57
1

This is quite elegant solution to this problem. It looks for global unique values even in the same array.

var array1 = ['124','10','100','190','1000'];
var array2 = ['124','100','190', '45'];
var array3 = ['124','100','175','19','1900'];

function getUniqueValues(...arrays) {
    const concatetedArray = arrays.flat();
  
    return arrays.reduce((accumulator, current) => {
      return [...accumulator, ...current.filter((currentItem) => concatetedArray.indexOf(currentItem) === concatetedArray.lastIndexOf(currentItem))];
    }, [])
  }

console.log(getUniqueValues(array1, array2, array3));
VLAZ
  • 26,331
  • 9
  • 49
  • 67
0

A solution is to use an object like this:

var array1 = ['124', '10', '100', '190', '1000'];
var array2 = ['124', '100', '190', '45'];
var array3 = ['124', '100', '175', '19', '1900'];

const obj = {};
for (let i = 0; i < array1.length; i++) {
  if (!obj[array1[i]]) {
    obj[array1[i]] = 0;
  }
  obj[array1[i]]++;
}

for (let i = 0; i < array2.length; i++) {
  if (!obj[array2[i]]) {
    obj[array2[i]] = 0;
  }
  obj[array2[i]]++;
}

for (let i = 0; i < array3.length; i++) {
  if (!obj[array3[i]]) {
    obj[array3[i]] = 0;
  }
  obj[array3[i]]++;
}
const uniques = [];
Object.keys(obj).forEach(key => {
  if (obj[key] == 1) {
    uniques.push(key);
  }
})
console.log(uniques);
VLAZ
  • 26,331
  • 9
  • 49
  • 67
Vasi Maruseac
  • 66
  • 1
  • 1
  • 8
  • when you repeating yourself in code... see if you can create a function and re-use it - always remember [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) – balexandre May 13 '21 at 17:08