数组去重
--数组去重函数
function removeRepeat(a)
local b = {}
for k,v in ipairs(a) do
if(#b == 0) then
b[1]=v;
else
local index = 0
for i=1,#b do
if(v == b[i]) then
break
end
index = index + 1
end
if(index == #b) then
b[#b + 1] = v;
end
end
end
return b
end
--遍历数组输出
function output(o)
for k,v in ipairs(o) do
print(k,v)
end
end
--测试
arr = {1,1,1,2,4,5,3,2,5,3,6}
narr = removeRepeat(arr)
table.sort(narr) --对数组排序
output(narr)
结果:
1 1 2 2 3 3 4 4 5 5 6 6