Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript how 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 stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #javascript #find #index #duplicate #elements #array
ADD COMMENT
Topic
Name
1+1 =