aboutsummaryrefslogtreecommitdiff
path: root/challenge-226/deadmarshal/lua/ch-2.lua
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-226/deadmarshal/lua/ch-2.lua')
-rw-r--r--challenge-226/deadmarshal/lua/ch-2.lua24
1 files changed, 24 insertions, 0 deletions
diff --git a/challenge-226/deadmarshal/lua/ch-2.lua b/challenge-226/deadmarshal/lua/ch-2.lua
new file mode 100644
index 0000000000..11a97a94bd
--- /dev/null
+++ b/challenge-226/deadmarshal/lua/ch-2.lua
@@ -0,0 +1,24 @@
+#!/usr/bin/env lua
+
+local function deduplicate_and_remove_zeros(t)
+ assert(type(t) == 'table','t must be a table')
+ local res,hash = {},{}
+ for i=1,#t do
+ if t[i] == 0 then goto continue end
+ if not hash[t[i]] then res[#res+1] = t[i] end
+ hash[t[i]] = true
+ ::continue::
+ end
+ return res
+end
+
+local function zero_array(t)
+ assert(type(t) == 'table','t must be a table')
+ local dedup = deduplicate_and_remove_zeros(t)
+ return #dedup
+end
+
+print(zero_array({1,5,0,3,5}))
+print(zero_array({0}))
+print(zero_array({2,1,4,0,3}))
+