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 TypeScript

1 Answer

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

  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: number[] = [1, 3, 6, 8, 12, 90];
const arr2: number[] = [2, 3, 5, 6, 7, 8, 96];

const result: number[] = get_unique_values(arr1, arr2);
console.log(result);


  
  
/*
run:
  
[1, 2, 5, 7, 12, 90, 96] 
  
*/

 



answered Feb 17, 2025 by avibootz

Related questions

1 answer 78 views
1 answer 73 views
1 answer 74 views
1 answer 140 views
1 answer 56 views
1 answer 62 views
1 answer 71 views
...