Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,926 questions

51,859 answers

573 users

How to get unique values from two arrays in Node.js

1 Answer

0 votes
function get_unique_values(arr1, arr2) {
  const set1 = new Set(arr1);
  const set2 = new Set(arr2);
  const result = [];

  arr1.forEach(item => {
    if (!set2.has(item)) {
      result.push(item);
    }
  });

  arr2.forEach(item => {
    if (!set1.has(item)) {
      result.push(item);
    }
  });

  result.sort((a, b) => a - b);
  
  return result;
}

const arr1 = [1, 3, 6, 8, 12, 90];
const arr2 = [2, 3, 5, 6, 7, 8, 12, 85, 96];

const result = get_unique_values(arr1, arr2);
console.log(result);



/*
run:

[
   1,  2,  5, 7,
  85, 90, 96
]

*/

 



answered Feb 17, 2025 by avibootz

Related questions

1 answer 71 views
3 answers 179 views
1 answer 109 views
1 answer 104 views
1 answer 87 views
1 answer 86 views
...