const names = ['John', 'Paul', 'George', 'Ringo', 'John'];
let unique = [...new Set(names)];
console.log(unique); // 'John', 'Paul', 'George', 'Ringo'
const names = ['John', 'Paul', 'George', 'Ringo', 'John'];
function removeDups(names) {
let unique = {};
names.forEach(function(i) {
if(!unique[i]) {
unique[i] = true;
}
});
return Object.keys(unique);
}
removeDups(names); // // 'John', 'Paul', 'George', 'Ringo'
//Other Mathod using Foreach and includes
let chars = ['A', 'B', 'A', 'C', 'B'];
let uniqueChars = [];
chars.forEach((c) => {
if (!uniqueChars.includes(c)) {
uniqueChars.push(c);
}
});
console.log(uniqueChars);
const names = ['John', 'Paul', 'George', 'Ringo', 'John'];
let unique = [...new Set(names)];
console.log(unique);
const allNames = ['Nathan', 'Clara ', 'Nathan', 'Fowler', 'Mitchell', 'Fowler', 'Clara ', 'Drake', 'Fowler', 'Clyde ', 'Mitchell', 'Clyde '];
function checkName(names) {
const uniqueName = [];
for (let i = 0; i < names.length; i++) {
const nameIndex = names[i];
if (uniqueName.includes(nameIndex) == false) {
uniqueName.push(nameIndex);
}
}
return uniqueName;
}
const uniqueNames = checkName(allNames);
console.log(uniqueNames);