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,907 questions

51,839 answers

573 users

How to remove duplicates from array in TypeScript

4 Answers

0 votes
function unique(arr: number[]) {
    arr = arr.concat().sort();
    for (let i = 1; i < arr.length; ) {
        if (arr[i - 1] === arr[i])
            arr.splice(i, 1);
        else
            i++;
    }
    return arr;
}
   
let arr = [5, 6, 3, 1, 1, 5, 5, 1, 7, 7, 7, 7];
  
arr = unique(arr);
  
for (let i = 0; i < arr.length; i++) 
    console.log(arr[i]);
  
  
  
  
/*
run:
  
1
3
5
6
7
  
*/

 



answered Dec 13, 2021 by avibootz
0 votes
let arr = ["php", "javascript", "php", "css", "php", "typescript", "typescript", "typescript"];
 
arr = Array.from(new Set(arr));
 
console.log(arr); 
 
 
 
 
/*
run:
 
["php", "javascript", "css", "typescript"]
 
*/

 



answered Dec 13, 2021 by avibootz
0 votes
let arr = ["php", "javascript", "php", "css", "php", "typescript", "typescript", "typescript"];
 
arr = [...new Set(arr)];
 
console.log(arr); 
 
 
 
 
/*
run:
 
["php", "javascript", "css", "typescript"]
 
*/

 



answered Dec 13, 2021 by avibootz
0 votes
let arr = [5, 6, 3, 1, 1, 5, 5, 1, 7, 7, 7, 3];
  
arr = arr.filter((item,index) => arr.indexOf(item) === index)
  
for (let i = 0; i < arr.length; i++) 
    console.log(arr[i]);
  
  
  
  
/*
run:
  
5
6
3
1
7
  
*/

 



answered Dec 13, 2021 by avibootz
...