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 就是去掉重复的新数组
这样说不知道你能不能看懂。
添加回答
举报