JS數組去重的十種方法

来源:http://www.cnblogs.com/yuanbo88/archive/2017/08/11/7345331.html
-Advertisement-
Play Games

一、前言: 我們在實際工作中,或者在面試找工作時,都會用到或者被問到一個問題,那就是“數組如何去重”。是的,這個問題有很多種解決方案,看看下麵的十種方式吧! 二、數組去重方式大彙總: Methods 1: 思路:定義一個新數組,並存放原數組的第一個元素,然後將元素組一一和新數組的元素對比,若不同則存 ...


一、前言:

我們在實際工作中,或者在面試找工作時,都會用到或者被問到一個問題,那就是“數組如何去重”。是的,這個問題有很多種解決方案,看看下麵的十種方式吧!

 

二、數組去重方式大彙總:

Methods 1: 思路:定義一個新數組,並存放原數組的第一個元素,然後將元素組一一和新數組的元素對比,若不同則存放在新數組中。

		function unique(arr){
			var res = [arr[0]];
			for(var i=1; i<arr.length; i++){
				var repeat = false;
				for(var j=0; j<res.length; j++){
					if(arr[i] === res[j]){
						repeat = true;
						break;
					}
				}

				if(!repeat){
					res.push(arr[i]);
				}
			}
			return res;
		}
		console.log('------------方法一---------------');
		console.log(unique([1,1,2,3,5,3,1,5,6,7,4]));
	

Methods 2: 思路:先將原數組排序,在與相鄰的進行比較,如果不同則存入新數組。

		function unique2(arr){
			var arr2 = arr.sort();
			var res = [arr2[0]];
			for(var i=1; i<arr2.length; i++){
				if(arr2[i] !== res[res.length-1]){
					res.push(arr2[i]);
				}
			}

			return res;
		}
		console.log('------------方法二---------------');
		console.log(unique2([1,1,2,3,5,3,1,5,6,7,4]));
	

Methods 3: 利用對象屬性存在的特性,如果沒有該屬性則存入新數組。

		function unique3(arr){
			var res = [];
			var obj = {};
			for(var i=0; i<arr.length; i++){
				if( !obj[arr[i]] ){
					obj[arr[i]] = 1;
					res.push(arr[i]);
				}
			}

			return res;
		}
		console.log('------------方法三---------------');
		console.log(unique3([1,1,2,3,5,3,1,5,6,7,4]));
	

Methods 4: 利用數組的indexOf下標屬性來查詢。

		function unique4(arr){
			var res = [];
			for(var i=0; i<arr.length; i++){
				if(res.indexOf(arr[i]) == -1){
					res.push(arr[i]);
				}
			}

			return res;
		}
		console.log('------------方法四---------------');
		console.log(unique4([1,1,2,3,5,3,1,5,6,7,4]));
	

Methods 5: 利用數組原型對象上的includes方法。

		function unique5(arr){
			var res = [];

			for(var i=0; i<arr.length; i++){
				if( !res.includes(arr[i]) ){ // 如果res新數組包含當前迴圈item
					res.push(arr[i]);
				}
			}

			return res;
		}
		console.log('------------方法五---------------');
		console.log(unique5([1,1,2,3,5,3,1,5,6,7,4]));
	

Methods 6: 利用數組原型對象上的 filter 和 includes方法。

		function unique6(arr){
			var res = [];

			res = arr.filter(function(item){
			  return res.includes(item) ? '' : res.push(item);
			});

			return res;
		}
		console.log('------------方法六---------------');
		console.log(unique6([1,1,2,3,5,3,1,5,6,7,4]));
	

Methods 7: 利用數組原型對象上的 forEach 和 includes方法。

		function unique7(arr){
			var res = [];

			arr.forEach(function(item){
			  res.includes(item) ? '' : res.push(item);
			});

			return res;
		}
		console.log('------------方法七---------------');
		console.log(unique7([1,1,2,3,5,3,1,5,6,7,4]));
	

Methods 8: 利用數組原型對象上的 splice 方法。

		function unique8(arr){
			var i,
				j,
				len = arr.length;

			for(i = 0; i < len; i++){
		        for(j = i + 1; j < len; j++){
		            if(arr[i] == arr[j]){
		                arr.splice(j,1);
		                len--;
		                j--;
		            }
		        }
		    }

			return arr;
		}
		console.log('------------方法八---------------');
		console.log(unique8([1,1,2,3,5,3,1,5,6,7,4]));
	

Methods 9: 利用數組原型對象上的 lastIndexOf 方法。

		function unique9(arr){
			var res = [];

			for(var i=0; i<arr.length; i++){
				res.lastIndexOf(arr[i]) !== -1 ? '' : res.push(arr[i]);
			}

			return res;
		}
		console.log('------------方法九---------------');
		console.log(unique9([1,1,2,3,5,3,1,5,6,7,4]));
	

Methods 10: 利用 ES6的set 方法。

		function unique10(arr){
			//Set數據結構,它類似於數組,其成員的值都是唯一的
			return Array.from(new Set(arr)); // 利用Array.from將Set結構轉換成數組
		}
		console.log('------------方法十---------------');
		console.log(unique10([1,1,2,3,5,3,1,5,6,7,4]));
	

 

三、其他:

有意思的是,當我把數組的長度變得很大的時候(如下所示),測試了一下不同方法的執行時間長短,會發現方法三、四、五、六、七相對來說會更有優勢,而方法八的執行速度似乎一直墊底。你也可以在你本地跑一下試試!

	//測試用的超大數組
	var arr = [1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1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,6,7,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6];

	//這裡只是舉例方法三(如果你把所有方法放在一起測試,會更直觀的看到執行時間的差異)
	var time3 = new Date().getTime();
	function unique3(arr){
		var res = [];
		var obj = {};
		for(var i=0; i<arr.length; i++){
			if( !obj[arr[i]] ){
				obj[arr[i]] = 1;
				res.push(arr[i]);
			}
		}

		return res;
	}
	console.log('------------方法三---------------');
	console.log( unique3(arr) );
	console.log('3所花時間: ' + (new Date().getTime() - time3));

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1 2 53 ...
  • h5-文本框 ...
  • 這個demo只是一個大概的思路,具體還需要根據情況來進行 上述代碼有一個地方動態設置需要動畫的ID值的代碼,下圖可以幫助你理解 ...
  • 基於 Vue 全家桶 (2.x) 製作的移動端音樂 WebApp ,一個媲美原生的移動端音樂 App,項目完整、功能完備、UI美觀、交互一流。 ...
  • 創建對象 工廠模式 function createPerson(name, age, job){ var o = new Object(); o.name = name; o.age = age; o.job = job; o.sayName = function(){ alert(this.nam ...
  • Write less, do more. 這便是jQuery的宗旨!jQuery,一個高效、精簡並且功能豐富的 JavaScript 工具庫。 想必,對於每一個前端開發者,一定用過jQuery吧!俗話說,學會jQuery,打哪兒都不怕!(瞎說的。。) jQuery也是我接觸過的第一個JavaScri ...
  • 寫在開始 一開始將自己 部署到 ,結果發現打開頁面速度有點慢,然後又將其同時部署到 ,實現雙線路訪問,國內解析記錄到 ,國外解析到 ,這樣確實網站的速度能提高不少,但是國內訪問因為是經過 ,所以打開網站會有廣告,這點不能容忍,於是想到自己的伺服器也還空閑著,於是想到可以部署到自己的伺服器上,折騰開始 ...
  • 上一篇文章《一個基於ES5的vue小demo》我們講瞭如何用ES5,vue-router做一個小demo,接下來我們來把它變成基於ES6+webpack的demo。 一、環境搭建及代碼轉換 我們先搭建一下vue 的開發環境,根據我的一篇隨筆《Vue開發環境搭建及熱更新》,我們一步步搭建開發環境,pr ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...