Hello,
I want to check if all values in an array are equal similar to the all method in python
Is there something similar or equivalent in LuaU?
Hello,
I want to check if all values in an array are equal similar to the all method in python
Is there something similar or equivalent in LuaU?
Use a for loop
e.g
local val = false
local count = 0
for i, v in pairs(array) do
if v == true then
count += 1
end
end
if count == #array then
val = true
end
local function AllValuesEqual(Array)
if type(Array) ~= "table" or #Array == 0 then
return
end
local Count = {}
for Index, Value in ipairs(Array) do
if not Count[Value] then
Count[Value] = 1
else
Count[Value] += 1
end
if Index == #Array then
if Count[Value] == #Array then
return true
else
return false
end
end
end
end
local Array1 = {"a", "a", "a"}
local Array2 = {1, 1, 1}
local Array3 = {true, true}
local Array4 = {"a", "b", "c"}
local Array5 = {1, 2, 3}
local Array6 = {true, false}
print(AllValuesEqual(Array1)) --true
print(AllValuesEqual(Array2)) --true
print(AllValuesEqual(Array3)) --true
print(AllValuesEqual(Array4)) --false
print(AllValuesEqual(Array5)) --false
print(AllValuesEqual(Array6)) --false