1
local a = {1, 2, 3}
local b = {1, 2, 3, 4, 5, 6}
-- I need b - a = {4, 5, 6}

I want to remove all elements from array b that are also present in array a. Looking for FASTEST solution.

4

3 Answers 3

4

Invert the smaller table into a hash and compare against it in a loop.

function invtab(t)
    local tab = {}
    for _, v in ipairs(t) do
        tab[v]=true
    end
    return tab
end

local a = {1, 2, 3}
local b = {1, 2, 3, 4, 5, 6}

local ainv = invtab(a)

-- To get a new table with just the missing elements.
local ntab = {}
for _, v in ipairs(b) do
    if not ainv[v] then
        ntab[#ntab + 1] = v
    end
end

-- To remove the elements in place.
for i = #b, 1, -1 do
    local v = b[i]
    if ainv[v] then
        table.remove(b, i)
    end
end
Sign up to request clarification or add additional context in comments.

2 Comments

Wow great. I forgot to mention that each array will be a SET in fact (there won't be duplicates) - do you think it is possible to speed up the code with this assumption?
Not that I can think of. Storing the sets as hashes in the first place will help as it means you can avoid the inversion step (which costs a table iteration, table creation, and table allocation(s)).
4

This code copies the duplicate keys over to array c

local a = {1, 2, 3}
local b = {1, 2, 3, 4,5 ,6}
local c = {}

for k,v in ipairs(b) do
    local foundkey = false
    for _k,_v in ipairs(a) do
        if _v == v then 
            foundkey = true 
        end
    end
    if foundkey then 
        table.insert(c,v)
    end
end

Or

local a = {1, 2, 3}
local b = {1, 2, 3, 4, 5, 6}

for k,v in ipairs(b) do
    local key = 0
    for _k,_v in ipairs(a) do
        if _v == v then 
            key = _k
        end
    end
    if key ~= 0 then 
        table.remove(a,key )
    end
end

-- Outputs a = {7}

or

local a = {1, 2, 3}
local b = {1, 2, 3, 4, 5, 6}
local c = {}

for k,v in ipairs(b) do
    local foundkey = 0
    for _k,_v in ipairs(a) do
        if _v == v then 
            foundkey = _k
        end
    end
    if foundkey == 0 then 
        table.insert(c,v)
    end
end
a = c

-- Output a = {4, 5, 6}

Comments

0

you can use stack here. when just compare two indexes that are not same push into stack. stack.

4 Comments

Can't do like that, because both arrays items might not be sorted, and length of "a" might be also greater than length of "b"
length of two arrays does't matter here.
you just choose one array e.g we have array named A and want to remove from it which is not in B so you just sort them using any algo but if the array is large then use quick sort and then finally do as i said at first.
Please edit your answer to change/add details, comments are not a good place to have an answer. Also please try to ensure your solution is likely to solve the problem, don't provide guesses if you might not be able to solve the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.