var arr = [\'test0\',\'test2\',\'test0\'];
Like the above,there are two identical entries with value \"test0\",how to check it most efficiently?
You can convert the array to to a Set
instance, then convert to an array and check if the length is same before and after the conversion.
const hasDuplicates = (array) => {
const arr = ['test0','test2','test0'];
const set1 = new Set(array);
const uniqueArray = [...set1];
return array.length !== uniqueArray.length;
};
console.log(`Has duplicates : ${hasDuplicates(['test0','test2','test0'])}`);
console.log(`Has duplicates : ${hasDuplicates(['test0','test2','test3'])}`);