2 回答

TA貢獻1036條經驗 獲得超461個贊
開始 ?i = 0; ?a[i] = 1 ? hash = {} ?result?=?[], 這時hash[a[i]] 也就是 hash[1] ?是不存在的。就會走if里面的代碼hash={1: true} ??result?=?[1],
? ? ? ? ?i =1 時; a[i] = 2 ????hash = {1:true} ?result?=?[1] ,這時hash[a[i]] 也就是 hash[2] ?是不存在的。就會走if里面的代碼hash={1:true,2:true} ?result?=?[1,2]
????????i=2時;?a[i] = 2 ? ?hash = {1:true,2:true} ?result?=?[1,2] ;?這時hash[a[i]] 也就是 hash[2]=true 。就不會走if里的代碼 ,這里原數組里的第二個2就忽略了,也就是第2個2是不會進到result?
??????i = 3時;?a[i] = 3 ??hash = {1:true,2:true} ?result?=?[1,2] ;?這時hash[a[i]] 也就是 hash[3] ?是不存在的 。就會走if里面的代碼hash={1:true,2:true,3:true} ?result?=?[1,2,3]
? 這樣一直下去 ?最后得到的?result ?就是去掉重復的新數組
這樣說不知道你能不能看懂。
添加回答
舉報