How do i check if 2 tables have the same content in them if they're not in the same order?

hello,
how would i check if 2 tables have the same things inside them if they’re not in the same order?

for example:

local table1 = {"hi","140","bro"}
local table2 = {"140","hi","bro"}
^ those would be the same

local table1 = {"hi","140","140"}
local table2 = {"140","bro","bro"}
^ and those wouldn't

thanks in advance! :slight_smile:

do u want all the contents to be the same or just some of them?
mbmb

@S0MBRX
wait what i dont get it

i just want to know how to know if the tables have the same content, just in different/same order

replied to myself by accident lol

This function should work:

local function testSame(one, two)
	for i,v in pairs(one) do
		if not table.find(two, v) then
			return false
		end
	end
	for i,v in pairs(two) do
		if not table.find(one, v) then
			return false
		end
	end
	return true
end

Hope this helps!

2 Likes

this is a way of doing it

local table1 = {"hi","140","bro"}
local table2 = {"140","hi","bro"}

local TableStatus = true
for I, V in pairs(table1)do 
	local Matches = 0
	for II, VV in pairs (table2)do 
		if TableStatus == false then 
			break 
		end
		if V == VV then 
			Matches += 1
		end
	end
	if TableStatus == false then 
		break 
	end
	if Matches >= 1 then 
		continue
	else
		TableStatus = false
	end
end

if TableStatus then 
	print("they are the same")
else
	print("they aint")
end
2 Likes

This would only tell you if the tables are exactly the same. They were asking how they could tell even if they were in a different order.

1 Like

well your wrong because as u can see if i run the code above (with the tables not in the same order) the outpuit is correct, output:
image
run it yourself if you want

local table1 = {"IM","IN","THE", "WRONG", "ORDER"}
local table2 = {"ORDER","WRONG","THE","IN","IM"}

local TableStatus = true
for I, V in pairs(table1)do 
	local Matches = 0
	for II, VV in pairs (table2)do 
		if TableStatus == false then 
			break 
		end
		if V == VV then 
			Matches += 1
		end
	end
	if TableStatus == false then 
		break 
	end
	if Matches >= 1 then 
		continue
	else
		TableStatus = false
	end
end

if TableStatus then 
	print("they are the same")
else
	print("they aint")
end

output:
image

Oh, I appear to have been mistaken. Sorry for my misunderstaning. Although, @pastelpieeee, I do think that there is a problem with both answers that you should keep in mind. If, for instance, “hi” appears twice in one of the tables but only once in the other, it will still say they match.

yeah, i noticed that while testing them, but i think it’s good enough for my needs

1 Like

fixed

i present the mightiest table identical identifier lol
@pastelpieeee
if like @GamingBroCoolkid328 said you want it to check if they are EXACTLY identical this code works

local table1 = {"IM","IN","THE", "WRONG", "ORDER"}
local table2 = {"ORDER","WRONG","THE","IN","IM"}

local TableStatus = true
for I, V in pairs(table1)do 
	local Matches = 0
	local ExpectedMatches = 0
	for III, VVV in pairs(table1)do
		if V == VVV then
			ExpectedMatches += 1
		end
	end
	for II, VV in pairs (table2)do 
		if TableStatus == false then 
			break 
		end
		if V == VV then 
			Matches += 1
		end
	end
	if TableStatus == false then 
		break 
	end
	if Matches == ExpectedMatches then 
		continue
	else
		TableStatus = false
	end
end

if TableStatus then 
	print("they are the same")
else
	print("they aint")
end
3 Likes

ur an actual god tysm
30charrrrrrrr

1 Like

A more efficient, less convoluted and straight to the point, clear solution in case anyone has trouble understanding the marked solution.

A summarization of what I’m doing here:

  1. loop through every element in table1
  2. check if each value is found in the second table table2
  3. if found, remove that value so that it is not counted again when checking whether a value exists in the second table
  4. when found, set a variable same to false to indicate the tables are not equal
local table1 = {"hi","140","bro"}
local table2 = {"140","bro","hi"}


local function isEqual(t1, t2)
	--// the below is just something I do, not needed with slight modification
	--// calling from the table library is likely faster
	setmetatable(t2, {__index=table});
	local same = false;
	if(#t1==#t2) then
		same = true
		for _, v in ipairs(t1) do
			if (not t2:find(v)) then
				same = false
				break;
			else
				--// found, so remove this now to avoid having found a value at the same index in the case of multiple values
				t2:remove(t2:find(v))
			end
		end
	end
	return same
end

local areEqual = isEqual(table1, table2)
print("table 1 and 2 are "..(areEqual and "same" or "different"))
2 Likes

Here is a short and simple solution for people who want it:

local function isTabEqual(tab1, tab2)
	if typeof(tab1) ~= typeof(tab2) then return end

	for i, v in pairs(tab1) do
		if typeof(v) == "table" then
			if not isTabEqual(v, tab2[i]) then return end
		else
			if tab2[i] ~= v then return end
		end
	end

	return true
end
1 Like