How to make this script more optimized

Hello, I recently was watching a YT video and saw this as a coding example so I went ahead and made it in Luau and it works, can anyone give me some pieces of advice on how to optimize code, and some bad practices you see or think I may not know of and etc., thanks for reading!

local numArray = {}
local NumSlots = 25

function FindDuplicateInArray(array)
	for numCount = 1, NumSlots do
		table.insert(array, math.random(NumSlots))
	end
	
	print(table.unpack(array))
	
	local PastNums = {}
	
	for _,key in ipairs(array) do
		if PastNums[key] then
			print(key.." was the duplicate number!")
			break
		else
			PastNums[key] = true
		end
	end
end

FindDuplicateInArray(numArray)

The script has been updated to include everyone’s replies, thank you in helping me figure out how to optimize code and make quicker and more concise code, thank you!

1 Like
  1. You can remove the wait()s because the functions aren’t intensive for small NumSlots like 25.

  2. You don’t have to check that PastNums ~= nil because we know it exists; you define it as a table and never override it.

  3. In the if statement within the for loop, you only use the currentNumber variable once. You can just insert key directly to the table. But, that’s also a readability thing to let the reader know what key is, so you can keep it if you like.

EDIT: Formatting

2 Likes

I went ahead and added in your suggestions and changes, great thinking, especially with 3. since I wasn’t even thinking about how it wasn’t really needed.

1 Like

Are you trying to find duplicates in a table? If so here is what I would do:

function FindDuplicate(t)
    local t2 = {}
    for i,v in pairs(t) do
        if table.find(t2, v) then
            return v; -- Or whatever you want to return
        else
            table.insert(t2, 1, v)
        end
    end
end

I haven’t actually tested this but I am fairly certain it should work

2 Likes

Although this is not an optimization, it is a logical error. You used numArray instead of the array argument passed in, which means if you used an array other than numArray, it would not work as you expect.

You are using the wrong indexing technique in the above for loop, you should be using table.find.

if table.find(PastNums, key) then
	print(key.." was the duplicate number!")
	break
else
	table.insert(PastNums, key)
end

Better yet, the more efficient option would be:

if PastNums[key] then
	print(key.." was the duplicate number!")
	break
else
	PastNums[key] = true
end

I squeezed in a few more optimizations like not checking == nil and not using elseif, but otherwise, I think this is pretty fast.

1 Like

sure:

function FindDuplicate(t)   -- Make our function. Arg t is going to be our table that we will look for duplicates in
    local t2 = {}   -- declare another table
    for i,v in pairs(t) do  -- loop through the table that was passed on
        if table.find(t2, v) then   -- if value exists in our temporary table then we have duplicate
            return v; -- Or whatever you want to return
        else    -- else we just insert the value and keep looping until we find duplicate
            table.insert(t2, 1, v)
        end
    end
end
1 Like

I can tell that it runs smoothly, quickly, and is a decent bit mroe concise than my previous script, thank you!

1 Like

Somehow I visibly notice that the speed of it it better, it froze for like half a second originally but now as soon as it physically can, I see it in the output with almost 0 delay!