Is there any way I could do this more efficiently?

Provide an overview of:

  • What does the code do and what are you not satisfied with?

The function below basically takes a table of strings and converts them into string .. X if there are duplicates of string found in the table. Here’s an example:

Calling the function:

local nameSet = {"Zombie", "Zombie", "Fast Zombie"}
local newNameSet = ABCTargets(nameSet)
print(newNameSet)

Expected to print:

{"Zombie A", "Zombie B", "Fast Zombie"}

(It doesn’t count stand-alone strings and only accounts for duplicates.)

Problem:
I feel like this takes too long for the client to execute and it will drag down the feel of swiftness in the UI

  • What potential improvements have you considered?

Make the server run it but that only seems to make it slower since I have to fire a remote function and stuff like that

  • How (specifically) do you want to improve the code?

I would like it to
A: Be neater
B: Be faster, if possible

Without further ado, here ya go:

function ABCTargets(dataSet)
	--if runService:IsServer() then
	if dataSet then
		local contenderSet = {}
		local editiedNames = {}
		local alphabet = {"A", "B", "C", "D", "E", "F", "G", "H"}
		for _, contender in dataSet do -- Raw Data -> Names
			table.insert(contenderSet, {contender["Name"], contender["ContenderIndex"], contender["Type"]})
		end
		for _, contender in contenderSet do -- Names -> Table segregation
			if editiedNames[contender[1]] then -- Name check
				table.insert(editiedNames[contender[1]], contender)
			else
				editiedNames[contender[1]] = {contender}
			end
		end
		table.clear(contenderSet)
		for _, contender in editiedNames do -- Table segregation -> Name giving
			if table.maxn(contender) > 1 then
				for alphabetIndex, newContender in contender do
					table.insert(contenderSet, {newContender[1] .. " " .. alphabet[alphabetIndex] or "Error", newContender[2], newContender[3]})
				end
			else
				for _, newContender in contender do
					table.insert(contenderSet, newContender)
				end
			end
		end
		return contenderSet
	end
	--[[elseif runService:IsClient() then
		local reqFunc = repStorage.RemoteFunctions.DoABCTargets
		local returnedData = reqFunc:InvokeServer(dataSet)
		return returnedData
	end]]
end

I tried to improve your code

local function ABCTargets(dataSet)
    if not dataSet then return end

    local alphabet = {"A", "B", "C", "D", "E", "F", "G", "H"}
    local editedNames = {}

    for _, contender in ipairs(dataSet) do
        local name = contender["Name"]
        if not editedNames[name] then
            editedNames[name] = {}
        end
        table.insert(editedNames[name], contender)
    end

    local contenderSet = {}

    for name, contenders in pairs(editedNames) do
        if #contenders > 1 then
            for index, contender in ipairs(contenders) do
                local suffix = alphabet[index] or "Error"
                table.insert(contenderSet, {name .. " " .. suffix, contender["ContenderIndex"], contender["Type"]})
            end
        else
            table.insert(contenderSet, contenders[1])
        end
    end

    return contenderSet
end
2 Likes

After a bit of tweaking, your fix seems to be faster, cleaner, and better!
Thanks!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.