<script type="text/javascript"> var dataArr = [1,3,33,3,5,1,4,3,4,5]; document.write("没去掉之前"); function unique(arr) { var result = [], hash = {}; for (var i = 0, elem; (elem = arr[i]) != null; i++) { if (!hash[elem]) { result.push(elem); hash[elem] = true; } } return result; } console.log(unique(dataArr)); function unique2(arr) { var result = [], hash = []; for (var i = 0, elem; (elem = arr[i]) != null; i++) { if (!hash[elem]) { result.push(elem); hash[elem] = true; } } return result; } console.log(unique2(dataArr)); function benunique(arr){ var result = []; var flag; for(var i=0;i<arr.length;i++){ flag = 0; for(var j=0;j<result.length;j++){ if(arr[i] == result[j]){ flag = 1; } } if(!flag){ result.push(arr[i]); } } return result; } console.log(benunique(dataArr)); </script>
时间: 2024-10-04 12:25:07