js unique

<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>

 

posted @ 2015-12-30 17:18  天道酬勤,坚持!  阅读(1711)  评论(0编辑  收藏  举报