How to remove repeated string values from a list

Gamora

This is a continuation of a previous question.

I have a fixed list in Lua which I am reassigning values for

local a = {"apple", "pear", "orange", "kiwi", "tomato"}
local map = {
  apple = "RD",
  pear = "GR",
  orange = "OG",
  kiwi = "GR",
  tomato = "RD",
  banana = "YL",
}
colours = {}
for index = 1, #a do
  table.insert(colours,map[a[index]or "OT")
end

Now I would either like to edit the existing script, or add some new script, to remove any repeated values.

My end result should be a table (colours) with no repeated values or empty strings, but I can't seem to think of a neat way to do this!

If it's not possible (or really messy) my second option would be to count the number unique values in the table.

Piglet

If you don't want to run over the entire table every time you add an element you can simply create a second table where you remember which colours have been listed yet. Simply use the colour as key.

local a = {"apple", "pear", "orange", "kiwi", "tomato"}
local map = {
  apple = "RD",
  pear = "GR",
  orange = "OG",
  kiwi = "GR",
  tomato = "RD",
  banana = "YL",
}

local listedColours = {}
local colours = {}
for _,colour in pairs(a) do
  colour = map[colour] or "OT"
  if not listedColours[colour] then
    table.insert(colours, colour)
    listedColors[colour] = true
  end      
end

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python: remove repeated values only if at end of list

Remove repeated sequence of integers from list in Python

How to remove repeated spaces from a string

How to remove values from a list of list in Python

How to remove duplicate from list of values

Remove values from list

How to count how many times a number is repeated in a list( and if a number is not used based on values from another list )?

How to remove all the elements of list from a string?

How to remove the repeated members in a simple list [ prolog ]

Remove repeated values from permutation

Add values of repeated items from list to a dictionary

How to remove a complete row when no match found in a column's string values with any object from a given list?

grep for repeated values from pattern list

How to remove spacific values from url string

How to get repeated tag values from an xml in string format (Java)

How to remove string from list?

How to remove repeated object values from arrayList Jquery/Javascript

How to remove repeated data from a list in angularjs?

Ruby how to remove repeated regex in string

how to remove space and repeated digit from string

How to sort by odd lines then remove repeated values?

Excel: How to extract repeated values from randomly ordered list?

How to remove repeated sequence of characters in a string?

struggling to avoid repeated values from the list comprehension

How to remove the first duplicate values from a List?

How do I remove string '' from python dictionary list values?

How I can use regex to remove repeated characters from string

How to remove repeated sentences from a string

Regex remove repeated +-*/ characters from a string in javascript