1114 Exercise

// Create an array
var arr = [1,2,3,2,2,1,3,4,2,5];

// Remove duplicate numbers
in the array // Get each element in the array
for (var i = 0; i <arr.length; i ++) {
//console.log(arr[i]);
/ * Get all elements after the current element * /
for (var j = i + 1; j <arr.length ; j ++) {
//console.log("---->"+arr[j]);
// determine whether the values ​​of two elements are equal
if (arr [i] == arr [j]) {
// If they are equal, it proves that there are duplicate elements, then delete the element corresponding to j
arr.splice (j, 1);
// After deleting the element where the current j is, the following elements will be automatically filled.//This
will not Will be comparing this element, I need to compare the element where j is located
// make j decrement
j--;
}
}
}

console.log (arr);

Guess you like

Origin www.cnblogs.com/xt888/p/12707239.html