Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript how to check the indexes of duplicates elements in an array

// A method to find a duplicate value in an array and the index of the duplicates
// array can be any data type
const arry = ["1", "2", "3", "1"];
let set = new Set();
for (let i = 0; i < arry.length; i++) {
  let pastSize = set.size;
  set.add(arry[i]);
  let newSize = set.size;
  if (pastSize == newSize) {
    console.log("The array has duplicates at index: ", i);
  }
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #javascript #check #indexes #duplicates #elements #array
ADD COMMENT
Topic
Name
4+4 =