how to check if elements in the array repeats in javascript code example

Example 1: javascript check if array has duplicates

function hasDuplicates(array) {
    return (new Set(array)).size !== array.length;
}

Example 2: js check if array of objects contains duplicates

const arrayOfObjCopy = [...arrayOfObj];

Object.keys(arrayOfObjCopy).forEach((key) => {
  arrayOfObjCopy[key] = JSON.stringify(arrayOfObjCopy[key]);
 });

const arrayOfObjhasDuplicates = uniq(arrayOfObjCopy).length != arrayOfObjCopy.length;

Tags:

Java Example