#How to Extract Duplicates from a table/string

146 messages · Page 1 of 1 (latest)

unique solar
#

How to Extract Duplicates in a table/string

#

How to Extract Duplicates from a table/string

crisp edge
#
local t = {1, 1, 4, 9, 12, 8, 6, 9, 9}

for _, number in t do
    local duplicates = {}
    for __, duplicate in t do
        if duplicate == number then 
            table.insert(duplicates, duplicate)
        end
    end
end
#

@unique solar

#

then concat

unique solar
crisp edge
#

wym

#

adds them up

#

can you send the whole code

#

i dont understand ur messy code

unique solar
#

oh sorry, it doesn't add them up but it actually gives this error got something mixed up my bad:

crisp edge
#

ok so u got a string

#

full of numbers?

#

seperated in commas?

unique solar
#

ye

crisp edge
#

alr

#

wait

unique solar
#

I have another thing that works, so it's aight if you don't know

#

until you figure something out I will return it to how it was

crisp edge
#

"1, 1, 4, 9, 12, 8, 6, 9, 9"

#

this would be

#

1199?

unique solar
#

no the duplicates would be shortened to just 1,9

crisp edge
#

alr

unique solar
#

but that works too I could just tweak it

crisp edge
#
local t = "1, 1, 4, 9, 12, 8, 6, 9, 9"
local characters = string.split(t, ",")
local duplicateString = ""
for _, v in characters do
    v = tonumber(v)
    local found = 0
    for _, dupe in characters do
        if tonumber(dupe) == v then
            found += 1
        end
    end
    if found >= 2 and not string.match(duplicateString, v) then
        duplicateString = duplicateString..v..","
    end
end
print(duplicateString)
#

here

#

lmk

#

if it woroks

unique solar
#

doesn't work:

#

I can send a vid if you want

crisp edge
#

cuz ur not calling the function

#

nor doing anything w it?

unique solar
unique solar
crisp edge
#

unselect

#

the

#

code

#

its hard to see

unique solar
#
Intersects.MouseButton1Click:Connect(function()
    local SetBox1 = CalculatorGui.Frame.Sets:WaitForChild("1")
    local SetBox2 = CalculatorGui.Frame.Sets:WaitForChild("2")
    local SetBox3 = CalculatorGui.Frame.Sets:WaitForChild("3")
    if one.BackgroundColor3 == Color3.new(1,0,0) and two.BackgroundColor3 == Color3.new(1,0,0)
        and three.BackgroundColor3 == Color3.new(1,0,0) then
        local FullSet1to2to3 = string.sub(SetBox1.Text, 1, -2)..","..string.sub(SetBox2.Text, 2, -2)..","..string.sub(SetBox3.Text, 2, -1)
        local Characters = {}
        local myString = FullSet1to2to3

        local characters = string.split(FullSet1to2to3, ",")
        local duplicateString = ""
        for _, v in characters do
            v = tonumber(v)
            local found = 0
            for _, dupe in characters do
                if tonumber(dupe) == v then
                    found += 1
                end
            end
            if found >= 2 and not string.match(duplicateString, v) then
                duplicateString = duplicateString..v..","
            end
        end
        
        if duplicateString ~= "" then
            Output.Text = "Output:\n ".."{"..duplicateString.."}"
        end    
    end    
end)```
crisp edge
#

print

#

FullSet1to2to3

unique solar
#

it doesn't execute due to this error:

Players.combinationto.PlayerScripts.LocalScript:508: invalid argument #2 to 'match' (string expected, got nil)

#

so you gotta fix it first

#

I could print somewhere else actually hold on

#

oh now that I did that,

FullSet1to2to3 when printed outputs: {1,2,3,4,1,3}

#
                local FullSet1to2to3 = string.sub(SetBox1.Text, 1, -2)..","..string.sub(SetBox2.Text, 2, -2)..","..string.sub(SetBox3.Text, 2, -1)
                print(FullSet1to2to3)
                local Characters = {}
                local myString = FullSet1to2to3

                local characters = string.split(FullSet1to2to3, ",")
                local duplicateString = ""
                for _, v in characters do
                    v = tonumber(v)
                    local found = 0
                    for _, dupe in characters do
                        if tonumber(dupe) == v then
                            found += 1
                        end
                    end
                    if found >= 2 and not string.match(duplicateString, v) then
                        duplicateString = duplicateString..v..","
                    end
                end

                if duplicateString ~= "" then
                    Output.Text = "Output:\n ".."{"..duplicateString.."}"
                    
                end    
            end    
        end)
    end
end)```
#

if you don't know it's fine, as I've said I already have another working method

crisp edge
#

no i do know

#

i just need debugging

unique solar
#

well you got the script lol

crisp edge
#

oh

#

change

#

v in characters

#

to

#

v in FullSet1to2to3

unique solar
#

Players.combinationto.PlayerScripts.LocalScript:491: attempt to iterate over a string value

#

errorline:
for _, v in FullSet1to2to3 do

#
    local FullSet1to2to3 = string.sub(SetBox1.Text, 1, -2)..","..string.sub(SetBox2.Text, 2, -2)..","..string.sub(SetBox3.Text, 2, -1)
        print(FullSet1to2to3)
        local Characters = {}
        local myString = FullSet1to2to3

        local characters = string.split(FullSet1to2to3, ",")
        local duplicateString = ""
        for _, v in FullSet1to2to3 do
            v = tonumber(v)
            local found = 0
            for _, dupe in characters do
                if tonumber(dupe) == v then
                    found += 1
                end
            end
            if found >= 2 and not string.match(duplicateString, v) then
                duplicateString = duplicateString..v..","
            end
        end

        if duplicateString ~= "" then
            Output.Text = "Output:\n ".."{"..duplicateString.."}"

                end    
            end    
        end)```
crisp edge
#

oh wait

#

wtf

#

im confused

#

how is ur textbox

#

supposed to look like

#

when u type in something

#

why are u doing subbing

unique solar
#

because it's two textboxes that look like this:

{1,2,3,4} - {2,3,4}

therefore the string.sub("string", 1, -2) and string.sub("string",2,-1)

#

to handle the curly brackets yk

crisp edge
unique solar
crisp edge
unique solar
crisp edge
#

cant u have

#

TextLabal TextBox TextLabel

#

and the textlabel

#

has text

#

of curly bracket?

unique solar
#

yeah but then this happens:

{1,2,3,4}{2,3,4}

#

not ideal

crisp edge
#

why tf

#

it prints right

#

but when u put

#

string in split

unique solar
#

yeah

crisp edge
#

it says its nil

#

give me code

unique solar
#

yours or mine

crisp edge
#
   local FullSet1to2to3 = "{1, 1, 1, 8, 10, 88, 40}"
 --local FullSet1to2to3 = string.sub(SetBox1.Text, 1, -2)..","..string.sub(SetBox2.Text, 2, -2)..","..string.sub(SetBox3.Text, 2, -1)
        print(FullSet1to2to3)
        local Characters = {}
        local myString = FullSet1to2to3

        local characters = string.split(FullSet1to2to3, ",")
        local duplicateString = ""
        for _, v in characters do
            v = tonumber(v)
            local found = 0
            for _, dupe in characters do
                if tonumber(dupe) == v then
                    found += 1
                end
            end
            if found >= 2 and not string.match(duplicateString, v) then
                duplicateString = duplicateString..v..","
            end
        end

        if duplicateString ~= "" then
            Output.Text = "Output:\n ".."{"..duplicateString.."}"

                end    
            end    
        end)
#

try this

#

you should get

unique solar
crisp edge
#

to see if it works

#

with custom

#

cuz its not supposed to be nil

#

if it prints its a string

unique solar
# unique solar

bro omg the damn problem is infront of your fucking eyes.

the error message says it loud and clear but you can't listen for shit

#

Fullset1to2to3 works perfectly fine it's something with it, it's your logic

crisp edge
#

THATS CUZ V IS NIL

buoyant aspenBOT
#

studio** You are now Level 11! **studio

crisp edge
#

MEANING CHARACTERS

#

IS ALSO nil

#

NOT A TABLE

#

YOU FUCKING IDIOT

#

DONT EVEN TRY TO SAY SHIT

unique solar
#

then why the fuck does my previous code work

#

nah you're just shit

crisp edge
#

says the one who cant figure out a duplicate method

#

lmao

unique solar
unique solar
#

you want proof?

#

cuz I am tired of your dumbass

crisp edge
#

then why is ur ass here?

#

if u got working method

#

cuz i tested mine

#

it works fine

unique solar
#

I thought wrong

#

I will send proof

crisp edge
#

piece of shit

#

learn lua

unique solar
#

learn basic brain usage

crisp edge
#

says the one who needs help figuring out to find duplicates

#

in table

unique solar
crisp edge
#

its different in things that studio has

#

but compiling is same

#

u fucking idiot

#

also

#

print ur characters table

unique solar
#
        local function extractDuplicates(str)
            local seen = {}
            local duplicates = {}
            for i = 1, #str do
                local char = str:sub(i, i)
                if char ~= "{" and char ~= "}" and char ~= "," then        
                    if seen[char] then
                        duplicates[char] = true
                    else
                        seen[char] = true
                    end
                end
            end
            return duplicates
        end

        local duplicates = extractDuplicates(myString)
        local duplicateString = ""
        for char, _ in pairs(duplicates) do
            duplicateString = duplicateString..","..char
            if duplicateString:sub(1,1) == "," then
                duplicateString = ""..duplicateString:sub(2,-1)
            end
        end
        if duplicateString ~= "" then
            Output.Text = "Output:\n ".."{"..duplicateString.."}"
        end    
    end    
end)```
unique solar
#

dumb shit

#

tired of you know it all wannabes

crisp edge
#

wtf even is that 😭

#

among us wannabe game

unique solar
crisp edge
#

its not random

#

it literally gives ideas

#

about among us mission

bronze rivetBOT
#

dynoSuccess shinyspatula has been warned. || asking for help in #1020374354867007528 and then being toxic to those who try to help u 💀

crisp edge
#

💀

#

LMAO