Remove same values from array containing multiple values JavaScript

Let’s say the following is our array with similar values −

const listOfStudentName = ['John', 'Mike', 'John', 'Bob','Mike','Sam','Bob','John'];

To remove similar values from array, use the concept of set(). Following is the code −


const listOfStudentName = ['John', 'Mike', 'John', 'Bob','Mike','Sam','Bob','John'];
console.log("The value="+listOfStudentName);
const doesNotContainSameElementTwice = [ Set(listOfStudentName)];
console.log("The Array=");

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo42.js.


This will produce the following output −

PS C:\Users\Amit\JavaScript-code> node demo42.js
The value=John,Mike,John,Bob,Mike,Sam,Bob,John
The Array= [ 'John', 'Mike', 'Bob', 'Sam' ]