How do I check if there are two or more different values in a table?

Hey there!
So I want to check if there are three different values in a table but I don’t know how.

for _, v in pairs(adventurerList) do
    for _, val in pairs(adventurerList) do
        if v ~= val  and adventurerCounter == 0 then
            print("Adventurer I")
            adventurerCounter = 1
        end
    end
end

I may be misunderstanding this, but what I would do is go through the table you are checking and add each value to a temporary table, each time you add a new value to the temporary table check if it already exists in the table. If it doesn’t then you can add it. You can also check the length of it each time to check if you have 3 values in the table.

Hope this helps you.

local Table = {'Adventurer1', 'Adventurer2', 'Adventurer2', 'Adventurer3'}

local function CheckTable(PassedTable)
	local TempTable = {}
	
	for _,Value in pairs(PassedTable) do
		if not table.find(TempTable, Value) then
			table.insert(TempTable, Value)
		else
			return(false)
		end
	end
	
	return(true)
end

print(CheckTable(Table))

This would return false because Adventurer2 is repeated, again, not sure if this is what you wanted the question is slightly confusing.

Not sure why you are looping through a table inside the same table, but I believe you are trying to get the number of elements inside a table. For that, you could either use #table or table.getn(table)

local adventurers = #adventurerList

Why are you trying to loop into the table you are looping in

What I’m trying to do is like if you have a table with for example {“Something”, “Another”} there are two different values so it will print like “2” and {“Something”, “Another”, “AnotherOne”} then it will print “3” but there can be like two of the same thing so I want to check only the number of the different things in the table. Sorry if it was too confusing.

oh, you mean how many objects are in an table? you could try setting up a variable with a counter

let’s say that adventurerList has two things in this example

local adventurerCount = 0

for _, v in pairs(adventurerList) do
    adventurerCount += 1
end

print(adventurerCount) -- should print 2!

No, only how many different objects are in a table. Duplicates don’t count.

hmm… i’d suggest the method that @r_apt used, basically just moving it to another table to store non-duplicate values

in this example, adventurerList has 4 objects, including 2 duplicates

local adventurerCount = 0

local nonDuplicates = {}

local function getNumObjects()
    for i, v in pairs(nonDuplicates)
        adventurerCount += 1
    end

    return(adventurerCount)
end) -- i forgot if you add a paranthesis or not

for_, v in pairs(adventurerList) do
    if not table.find(nonDuplicates, v) then
        table.insert(nonDuplicates, value)
    end
end

local objectNum = getNumObjects()
print(objectNum) -- should be 3!

I’ll try that out after school. I think that’s what I’ve been looking for, thanks!

1 Like

Once I get back and it’s working I’ll be sure to mark your answer as the solution

1 Like

shouldn’t you mark the other guy’s (@r_apt) post? it was his idea lol