This will take up a bit of memory but will be exponentially faster than searching a list.
Slight modification where we no longer have the Item field in ItemClass as we don’t need an index, and I changed the n1, n2, n3 into an array as all n’s have the same properties.
public class Program
{
public class ItemClass
{
public string Date;
public int[] n;
}
private Dictionary<int, List<ItemClass>> items = new Dictionary<int, List<ItemClass>>();
public bool NumberHasBeenUsed(int value)
{
return items.ContainsKey(value);
}
public ItemClass CreateNewItem(int[] numbers)
{
ItemClass item = new ItemClass();
item.n = numbers;
for (int i = 0; i < numbers.Length; i++)
{
var value = numbers*;*
List list = null; if (NumberHasBeenUsed(value)) { list = items[value]; } else { list = new List(); items.Add(value, list); } list.Add(item); }
return item; }
public void Main() { List allitems = new List(2000); for (int i = 0; i < 2000; i++) { int[] n = new int[] { Random.Range(0, 100), Random.Range(0, 100), Random.Range(0, 100) }; ItemClass item = CreateNewItem(n); allitems.Add(item); }
foreach (KeyValuePair<int, List> pair in items) { Debug.Log("We have " + pair.Value.Count + “x of the number '” + pair.Key + “'”); } } } Output: [86645-2017-01-25.png|86645]*
*