JAVASCRIPT
javascript remove duplicates
var arr = ["apple", "mango", "apple", "orange", "mango", "mango"];
function removeDuplicates(arr) {
return arr.filter((item,
index) => arr.indexOf(item) === index);
}
console.log(removeDuplicates(arr)); // ["apple", "mango", "orange"]
javascript remove duplicate letters in a string
function removeDuplicateCharacters(string) {
return string
.split('')
.filter(function(item, pos, self) {
return self.indexOf(item) == pos;
})
.join('');
}
console.log(removeDuplicateCharacters('baraban'));
remove the duplicate character in string js
function removeDuplicate(str, letSingleChar = false) {
let string = str;
for (element of string) {
const re = new RegExp(element, 'g');
if (string.match(re)?.length > 1) {
if (letSingleChar == true) {
string = [...new Set(string.split(''))].join('');
} else
string = string.replace(re, '');
}
}
return string;
}
console.log(removeDuplicate("Hello World", true)) // Helo Wrd
console.log(removeDuplicate("Hello World", false )) // He Wrd
remove repeated characters from a string in javascript
const unrepeated = (str) => [...new Set(str)].join('');
unrepeated("hello"); //➞ "helo"
unrepeated("aaaaabbbbbb"); //➞ "ab"
unrepeated("11112222223333!!!??"); //➞ "123!?"
javascript remove duplicates
uniq = [...new Set(array)];
JavaScript remove duplicate items
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
// usage example:
let a = ['a', 1, 'a', 2, '1'];
let unique = a.filter(onlyUnique);
console.log(unique); // ['a', 1, 2, '1']
remove duplicates javascript
function withoutDuplicates (arr) {
return [...new Set(arr)];
}
remove duplicate values from string in javascript
const names = ['John', 'Paul', 'George', 'Ringo', 'John'];
let unique = [...new Set(names)];
console.log(unique);
remove duplicate name from array javascript
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);
how to remove duplicates in js
const names = ['John', 'Paul', 'George', 'Ringo', 'John'];
let unique = [...new Set(names)];
console.log(unique);