Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Javascript find the index of duplicate elements in 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 gist.github.com #
 
PREVIOUS NEXT
Tagged: #Javascript #find #index #duplicate #elements #array
ADD COMMENT
Topic
Name
7+9 =