Help with algorithm which checks for a table with 2 specific values and returns true if it found the table

Hello there! I need a help with an algorithm.
I need a function which will check if there’s a table with 2 specific numbers in a big list. These tables look like this {x,z} where x and z are integers. And I need the fast algorithm. Because looping through all tables takes way too much time.

Would something like this work for what you’re wanting? :slight_smile:

local t1 = {1,2,3} 
local t2 = {3,4,5} 
local num = 3 
local exists = table.find(t1,num) and table.find(t2,num)

print(exists)

Sadly no. I need to find a table among other tables. It’d look something like this:

local list= {{1,1}, {3,6}, {9,1}, {12,6}}
local tableToFind={3,7}
--Should return false because there's no such a table

You could use JSONEncode to make the table entries strings and then do something like this perhaps?

local HttpService = game:GetService('HttpService')

local t1 = {
	HttpService:JSONEncode({1,2}),
	HttpService:JSONEncode({2,3}),
	HttpService:JSONEncode({3,4})
} 
local t2 = {
	HttpService:JSONEncode({3,4}),
	HttpService:JSONEncode({4,5}),
	HttpService:JSONEncode({5,6})
} 

local num = HttpService:JSONEncode({3,4}) 
local exists = table.find(t1,num) and table.find(t2,num)

print(exists)

Aren’t you able to use the index of the tables to find them?
What are you trying to do, specifically?

Basically, I have a list of coordinates with 2 axis. And I have a function which returns true if there’s already a list of the same coordinates. These coordinates represent somewhat called cells, and this function meant to prevent spawning 2 different things on one cell

local list = {{1,1}, {3,6}, {9,1}, {12,6}}
local function find(tableToFind)
	for _, v in pairs(list) do
		if v[1]~=tableToFind[1] or v[2]~=tableToFind[2] then continue end
		return true
	end
	return false
end
local check = find({3,7})

Time to process 10.000.000 tables: ~0.00057s
image
What is the essence of the code: it does not go through ALL options, as soon as it finds the right one - it will return true, and there is also no comparison and, I use or to spend less time, because if 1 is not performed, then there is no point in checking 2.

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