Renaming a series of numbers based on a duplicate number?

Not sure quite how to explain this, but let me try to make it easier to visualize. I have a list with 6 different numbers, like so:

1, 2, 3, 4, 5, 6

. Say I duplicate the number 3, I now have this list (with two 3s!):

1, 2, 3, 3, 4, 5, 6.

I now want to rename the rest of those numbers to be:

1, 2, 3, 4, 5, 6, 7.

As you can see, the numbers 3-6 now become 4-7.
Honestly I have no idea how to go about this unless I did a number of if then statements to detect which numbers to change, but at a large scale that method would be very inefficient.

Any advice?

Thanks so much,
korky5000

I just wrote this real quick. Let me know if it solves your problem.

local nums = {1,2,3,3,4,5};

for a,b in pairs(nums) do
	for c,d in pairs(nums) do
		if b == d and a ~= c then
			table.remove(nums, c);
			table.insert(nums, #nums+1);
		end
	end
end

Here is the output.

This can be broken with a sequence:

local nums = {1,2,3, 3, 3, 6, 4,5};

(output)
image

Here’s some code that works

local nums = {1, 2, 3, 3, 3,4, 5, 6, 2, 4, 7, 9}
print(#nums)
repeat
	local FoundADuplicate = false -- This current pattern of nums doesn't have a duplicate yet (we don't need to loop through it)
	-- Count how many times each number appears
	for i,v in pairs(nums) do
		local Count = 0
		for _, q in pairs(nums) do
			if q == v then
				Count = Count + 1
			end
		end
		
		if Count > 1 then
			-- It appears more than once, for each time it appears, we will add a number to the one above
			FoundADuplicate = true
			-- We need to remove 1 from this number
			repeat
				local Subtracted = false -- A safety measure to make sure we don't accidentally put more than 1 duplicate up at once, without counting
				for _, q in pairs(nums) do
					if q == v and not Subtracted then -- This is a duplicate and it's the first time we're removing a duplicate in this sequence
						-- remove
						nums[_] = q + 1
						Subtracted = true
						Count = Count - 1
					end
				end
			until Count == 1 -- There is no more duplicates
		end
	end
until FoundADuplicate == false -- Run through the above code again, adding numbers one by one..

table.sort(nums, function(a, b) return a < b end)

print(unpack(nums))

There most likely is a way to make this much more efficient.

Output:image

1 Like

If he is using sequences, then yes that would be the solution.

I just wrote this up for you. Hopefully it helps.

function findDuplicates(t)
   local seen = {} --keep record of elements we've seen
   local duplicated = {} --keep a record of duplicated elements
    for i = 1, #t do
      local  Value = t[i]  
        if seen[Value] then  --check if we've seen the element before
            duplicated[Value] = true --if we have then it must be a duplicate! add to a table to keep track of this
	        table.remove(t, t[i]) -- removeing it from the table
	        table.insert(t, Value + 1) -- adding the new value in
        else
            seen[Value] = true -- set the Value to seen; to detect if its duplicated in the future
        end
    end 
    return duplicated
end 

Table = {1, 1, 2, 3, 3, 4, 5, 6, 6, 6, 6}
for key,_ in pairs(findDuplicates(Table)) do
    print("Old Table: "..key) -- prints all the duplicated Numbers
end


wait(3)  -- This is just so you can see the new table in the output
for key,_ in pairs(Table) do
 print("New Table: "..key) -- prints the new table
end

Output

Pictura

1 Like

You should put your code in codeblocks, like this:
```
– code
```

1 Like

If I’m reading this correctly, this should be doable in O(n) (assuming the list is sorted). I’m not sure how to handle each case though. If you have:

1, 2, 3, 3, 6, 7

What should the result be?
What about with:

1, 2, 3, 3, 3, 3, 3, 3, 6

My idea is something like this:

function foo(list)
if (#list < 1) then
return {}
end
local newList = {list[1]}
local dups = 0
for i = 2, #list, 1 do
local newN = list[i]
if list[i] == list[i - 1] then
dups = dups + 1
end
newList[i] = newN + dups
end
return newList
end

What do you mean “renaming” numbers? Do you just have a table of numbers? Why do you need it? Knowing more about your specific situation makes it easier to help.

1 Like