Find the missing number of table

Dont flag this post there is not much to say about:

This is what I created untill now but I cant find the missing number how can I find the missing number of the Table?

local numbers = {1,2,3,4,5,6,7,9}

function getMissingNumber(a, b)
	local total = (b +1) * (b + 2)/2;
	for i = 1, #a do
		total  -= a[i]
	end
	return total 
end

local miss = getMissingNumber(numbers, 5)
print(miss)

So we want find the number 8 that is missing in our table and this is just possible with the formula
n (n+1)/2*

btw:

1 Like

Can you give me a better example on what you’re trying to achieve?

What is b?
What is the missing number you want to find.

The length of the array is n-1. So the sum of all n elements, i.e sum of numbers from 1 to n can be calculated using the formula n(n+1)/2*

Here is the whole post:

And the script should find the number, so I can change what I want.

I don’t know if this is what you are looking for, do you want to find all the missing numbers in a table?

local numbers = {1,2,3,4,5,6,7,9}

function getMissingNumber(theTable, minNumber, maxNumber)
	local MissingTable = {}
        for i = minNumber, maxNumber do
             if not theTable[i] then
                         table.insert(MissingTable, i)
             end
        end
	return MissingTable
end

local miss = getMissingNumber(numbers, 1, 10) 
print(miss)
--first parameter Table, second is minimum, third is maximum

Oh I am a real idiot. I forgot I could put 2 parameter in it.

I am sorry but your script didnt work and it doesnt seem like it could. But your Idea is good with 2 parameters

I don’t really understand what you want. You want to look through a table for missing numbers? Like if I had this table:

{1,2,4,5,7,8,9}

You want it to see that “3 and 6” are missing??

Yes thats true I want see 3 and 6 are missing

but when I change the table, the script should figure out how much is missing each time when I change the table and thats just working with the formula:

formula n (n+1)/2*

So please keep working with it.

So, I solved it.

btw I didnt solved it just understand a forum about it.

so for the table we need everytime the length of the array and that is n-1 so we do actually everytime with the second number under the max table value.

local numbers = {1,2,3,5,6,7,8,9}

function getMissingNumber(a, b)
	local total = (b +1) * (b + 2)/2;
	for i = 1, #a do
		total  -= a[i]
	end
	return total 
end

local miss = getMissingNumber(numbers, 8)
print(miss)

----output = 4

Yes as I said! I just wanted find the one number not a ton of numbers

And actually my script does work so there is not anything I need to do.

and actually if it doesnt exist why does my script work then :face_with_raised_eyebrow: :expressionless:
So everything what you said doesnt seem true.

and please dont copy and paste a script thanks! You can already see it in the solution

I’ve just found out they added these operators in roblox lua, but it is not a standard. If you try it in another lua interpreter, -= and += won’t work.

@Soflowsen please think before you write. And please delete your text above because of your text people get just a misconception about roblox lua operators. And nobody talked about other languages we are still in this devforum so dont switch over languages, Thanks!!

I know you’ve found a solution, but personally I wouldn’t use an algorithm. I just feel it’s easier and more convenient to work with some scenario logic given the way you’re currently doing it is a bit hacky and may not always evaluate correctly. There’s also factors such as negative numbers where this method may or may not give the appropriate results.

Assuming that each missing number is exactly 1 whole number away, you can take advantage of this by adding 1 to each number, and seeing if it returns the next number. If it doesn’t you have a missing number. By no means am I saying it’s bad to work with a formula or that you’re incorrectly solving the problem, just wanted to show how you can take advantage of a given scenario, by finding something common :slight_smile:

local numbers = {1,2,3,5,6,7,8,9}
local max, min = math.max(unpack(numbers)), math.min(unpack(numbers))

function getMissingNumber(theTable, minNumber, maxNumber)
	for i, v in ipairs(numbers) do
		if i < #numbers and v > 0 and numbers[i + 1] ~= v + 1 then -- Positive integers
			return v + 1
		elseif i < #numbers and v < 0 and numbers[i + 1] ~= v + 1 then -- Negative integers
			return v - 1
		end
	end
end

getMissingNumber(numbers, min, max)
2 Likes