Code
<Script type = "text/javascript">
Array. prototype. distinct = function (){
Var $ = this;
Var o1 = {}; // store the deduplication Value
Var o2 ={}; // store duplicate values
Var o3 = []; // store duplicate values
Var o; // a single variable in the array
For (var I = 0; o = $ [I]; I ++ ){
If (o in o1 ){
If (! (O in o2) o2 [o] = o;
Delete $ [I];
} Else {
O1 [o] = o;
}
}
$. Length = 0; // clear the original array
For (o in o1 ){
$. Push (o );
}
For (o in o2 ){
O3.push (o );
}
Return o3;
}
Var a = [2, 2, 3, 3, 4, 5, 6, 7];
Alert ("original array:" + );
Alert ("repeated elements:" + A. distinct (); //,
Alert ("the sorted array is:" + a); // 5.6, 4, 6
Alert ("the length after sorting is:" + A. Length) // 6
</SCRIPT>
A peer is puzzled by the above program code, mainly because he does not understand the following code:
For (var I = 0; o = $ [I]; I ++ ){
If (o in o1 ){
If (! (O in o2) o2 [o] = o;
Delete $ [I];
} Else {
O1 [o] = o;
}
}
Finally, we will discuss it as follows: