哈希表(Hashtable)這個概率應該是#c里面的概念,用來賦值、取值、遍歷、排序操作提高效率。想起這個東西其實使我們以前經常遇到這樣的面試題,一個很大的數組可能有100000個,如何快速知道它里面的出現最多的次數,那么這里我們可能就要用Hashtable的相關知識了。Javascript中,object的實現就是hash表,因此只要在object上封裝點方法,再利用原生的hasOwnProperty方法就可以實現簡單高效的hashtable。
一,什么是哈希表(Hashtable)
二,哈希表的簡單操作
三,js模擬哈希表的簡單操作
一,什么是哈希表(Hashtable)
Framework中,Hashtable是System.Collections命名空間提供的一個容器,用於處理和表現類似key/value的鍵值對,其中key通常可用來快速查找,同時key是區分大小寫;value用於存儲對應於key的值。Hashtable中key/value鍵值對均為object類型,所以Hashtable可以支持任何類型的key/value鍵值對。
二,哈希表的簡單操作C#
在哈希表中添加一個key/value鍵值對:
HashtableObject.Add(key,value);
在哈希表中去除某個key/value鍵值對:
HashtableObject.Remove(key);
從哈希表中移除所有元素:
HashtableObject.Clear();
判斷哈希表是否包含特定鍵key:
HashtableObject.Contains(key);
三,js模擬哈希表的簡單操作
HashTable.prototype = { contructor:HashTable, //初始化 initialize:function(){ this.obj = {}; }, //獲取hashTable中對象唯一出現的次數 count:function(){ var count = 0; for(var i in this.content) count++; return count; }, //返回hashTable中的值 items:function(key){ if(this.contains(key)){ return this.obj[key]; } }, //增加值到hashtable add:function(key,value){ if(this.obj.hasOwnProperty(key)){ return false; }else{ this.obj[key] = value; return true; } }, //清空hashtable中的值 clear:function(){ this.obj = {}; }, //檢測hashTable對象中是否含有此屬性 contains:function(key){ return this.obj.hasOwnProperty(key); }, //移除hashTable中對象的值 remove:function(key){ delete this.obj[key]; } }
這樣我們就能像c#語言里面的那樣進行操作了。
還一個簡單的變體版:
// js哈希表 function HashTable() { this.ObjArr = {}; this.Count = 0; //添加 this.Add = function(key, value) { if (this.ObjArr.hasOwnProperty(key)) { return false; //如果鍵已經存在,不添加 }else { this.ObjArr[key] = value; this.Count++; return true; } } //是否包含某項 this.Contains = function(key) { return this.ObjArr.hasOwnProperty(key); } //取某一項 其實等價於this.ObjArr[key] this.GetValue = function(key){ if (this.Contains(key)) { return this.ObjArr[key]; }else { throw Error("Hashtable not cotains the key: " + String(key)); //腳本錯誤 //return; } } //移除 this.Remove = function(key) { if (this.Contains(key)) { delete this.ObjArr[key]; this.Count--; } } //清空 this.Clear = function(){ this.ObjArr = {}; this.Count = 0; } } //員工 function employee(id, userName) { this.id = id; this.userName = userName; } function test() { var ht = new HashTable(); var tmpEmployee = null; for (var i = 1; i < 6; i++) { tmpEmployee = new employee(i, "Employee_" + i); ht.Add(i, tmpEmployee); } for (var i = 1; i <= ht.Count; i++) { alert(ht.GetValue(i).userName); //其實等價於ht.ObjArr[i].userName //alert(ht.ObjArr[i].userName); } ht.Remove(1); alert(ht.Contains(1)); //false alert(ht.Contains(2)); //true //alert(ht.GetValue(1)); //異常 var result = ht.GetValue(2); if (result != null) { alert("Employee Id:" + result.id + ";UserName:" + result.userName); } ht.Add(2, "這一個key已經存在!"); //Add無效 //ht.Clear(); //清空 alert(ht.Count); }
最后解決一下,開頭說的那個問題
Array.prototype.maxNum = function(){ var arr = this,obj={}; for(var i =0, len=arr.length;i<len;i++){ var key = arr[i]; if( ! obj[key]){ obj[key] = 1; }else{ obj[key]++; } } var max = -1,maxStr; for( key in obj){ if(obj[key]>max){ max = obj[key]; maxStr = key; } } //alert(maxStr); return [maxStr,max]; }