Checking if 4 different arrays share the same value

Let’s say these are the arrays I need to compare

local Array1 = {0, 1, 2, 3, 4}
local Array2 = {0, 2, 3, 4}
local Array3 = {1, 3, 4}
local Array4 = {2, 3, 4}

What I need, it’s have ONLY the values that repeat on all 4 of the arrays, I’ve been trying to figure out how to do it for a while but I have no idea, I’d appreciate some help.

Just keep doing for loops and checks till all of them share the same values?
Or you can use metatables.

You should find the array with the least amount of arguments, example array 3 or 4, and use table.find(otherArray, number) for each of that array’s number. Example:

local Array1 = {0, 1, 2, 3, 4}
local Array2 = {0, 2, 3, 4}
local Array3 = {1, 3, 4}
local Array4 = {2, 3, 4}

local sharedNumbers = {}
local smallestArray = Array4 -- if needed, use some function to find it but for testing purposes I won't
for _, value in smallestArray do
	local existsIn = 0
	for _, otherArray in {Array1, Array2, Array3} do
		local exists = table.find(otherArray, value)
		if exists then
			existsIn += 1
		end
	end
	if existsIn == #{Array1, Array2, Array3} then
		table.insert(sharedNumbers, value)
	end
end
2 Likes

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