Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript find duplicates in array using for loop

var inputs = [4, 2, 5, 2, 3, 4, 4, 5, 6, 7, 1, 3, 6, 8];
var duplicates = [];
var result = 0;
for (var i = 0; i < inputs.length; i++) {
  for (var c = 0; c < inputs.length; c++) {
    if (inputs[i] == inputs[c]) {
      result = result + 1;
      if (result == 2) {
        if (duplicates.indexOf(inputs[c]) !== -1) {
        } else {
          duplicates.push(inputs[c]);
        }
      }
    }
  }
  result = 0;
}

console.log(duplicates.sort());
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #javascript #find #duplicates #array #loop
ADD COMMENT
Topic
Name
4+8 =