这个是百度前端(web开发工程师)的一个面试题
编写一个方法,去掉一个数组的重复元素
<script type="text/javascript"> Array.prototype.strip=function(){ if(this.length<2)return this; var newArr = []; var move = function(obj){ newArr.push(obj[0]); obj.splice(0,1); for(var i=0;i<obj.length;i++){ if(obj[i] == newArr[newArr.length-1]){ obj.splice(i,1); i--; } } } while(this.length>0){ move(this); } return newArr; } var arr=["abc",85,"abc",85,8,8,'c',1,2,5,4,8,'d','c']; alert(arr.strip()); </script>
转载请注明来源: 百度面试题:编写一个方法,去掉一个数组的重复元素 - 朱凯博客
支付宝
微信