How to get only the unique words from a string in Node.js

2 Answers

0 votes
function uniqueArray(array) {
    array.sort();
   
    return array.filter(function(s,i,a) { return a[i + 1] != s; });
}
 
 
const s = "node.js c c++ node.js c++ node.js c php java";
 
let array = s.split(" ");
array = uniqueArray(array);
 
const uniqueString = array.join(' ');
console.log(uniqueString);
 
 
 
 
 
/*
run:
 
c c++ java node.js php
 
*/

 



answered Feb 12, 2022 by avibootz
0 votes
const s = "node.js c c++ node.js c++ node.js c php java";
 
let array = s.split(" ");
array = [...new Set(array)];
 
const uniqueString = array.join(' ');
console.log(uniqueString);
 
 
 
 
 
/*
run:
 
node.js c c++ php java
 
*/

 



answered Feb 12, 2022 by avibootz
...