<script language="javascript">
var arr=new Array(2,3,23,32,32,23,56,73);
var temp;
document.write("去掉重复的数以前:")
document.write(arr)
for(var i=0;i<arr.length-1;i++)
{
 for(var j=i+1;i<arr.length;j++)
{
if(arr[i]==arr[j]){
   temp=arr[length-1];
   arr[length-1]=arr[i];
   arr[i]=temp;
   
  }
 arr.pop( );
}
}
document.write("去掉重复的数以后:")
document.write(arr);
</script>

解决方案 »

  1.   

    <script language="javascript">
    var arr=new Array(2,3,23,32,32,23,56,73);
    var temp;
    document.write("去掉重复的数以前:")
    document.write(arr)
     Array.prototype.unique = function() 
      { 
        var a = {}; 
         for(var i=0; i<this.length; i++) 
        { 
          if(typeof a[this[i]] == "undefined") 
            a[this[i]] = 1; 
        } 
        this.length = 0; 
        for(var i in a) 
          this[this.length] = i; 
        return this; 
      } 
    arr.unique()
    document.write("去掉重复的数以后:")
    document.write(arr);
    </script>
      

  2.   


    Array.prototype.unique = function() {
        var temp = this.slice(0).sort();
        var index = 0;
        while(index < temp.length - 1) {
            if(temp[index] == temp[index + 1]) {
                splice(index, 1);
            } else {
                index += 1;
            }        
        }
        return temp;

    var arr=new Array(2,3,23,32,32,23,56,73);
    arr = arr.unique();