Combining Items in a single list by comparison

Hi I have a list of items in which a bunch of them are effectively duplicates. I need to compare each item to every other item to see if the they are a match on the correct fields e.g. for a Transform (although my items are not MonoBehaviours)do 2 transforms share the same name, scale and rotation [ignoring position etc] I’ve tried reverse looping but it seams to miss many items and cant seam to find another way to do it without causing an infinite loop here’s what I’m sort of using at the moment

		for (int i = Items.Count - 1; i > 0; i--)
		{
			for (int j = Items.Count - 1; j > 0; j--)
			{
				if (Items<em>.A == Items[j].A && Items_.B == Items[j].B && Items*.C == Items[j].D)*_</em>

* {*
_ Item b1 = Items*;
Item b2 = Items[j];
Item b = new Item ( b1.A,b1.B,b1.C,b1.D+b2.D);
Items.Insert (0, b);
Items.Remove (b1);
Items.Remove (b2);
i–;
j–;
}
}
}*

I’ve been searching for a solution and seam to find a lot of things mentioning “LinQ” but until a few hours ago id never heard of it and the tutorials I’ve found don’t seam to be have any knowledge I can easily rip out to make this work. Also just to note this wont be running to often [maybe 1-2 times in extreme cases] so shouldn’t be to much of a problem if the solution is resource heavy. any help would be greatly appreciated._

I think the last paragraph of your question got swallowed up by the code section.

Thanks should be readable now :D

1 Answer

1

UPDATE
This ended up being more in depth than I planned, but here’s how you can use two lists to do the combination as you have set up.

List<Object> toRemove;
List<Object> toAdd;

for(int i = 0; i < originalList.Count; ++i)
{
	Object obj1 = originalList*;*
  • if (toRemove.Contains(obj1))*
  •  continue;*
    
  • bool beenAdded = false;*
  • Object newVal;*
  • for(int j = i + 1; j < originalList.Count; ++j)*
  • {*
  •  Object obj2 = originalList[j];*
    
  •  if (ShouldCombine(obj1, obj2))*
    
  •  {*
    
  •  	toRemove.AddUnique(obj2);*
    
  •  	if (!beenAdded)*
    
  •  	{*
    
  •  		toRemove.AddUnique(obj1);*
    
  •  		newVal = Combine(obj1, obj2);*
    
  •  	}*
    
  •  	else*
    
  •  		newVal = Combine(newVal, obj2);*
    

beenAdded = true;

  •  }*
    
  • }*
  • if (beenAdded)*
  •  toAdd.Add(newVal);*
    

}

foreach(Object removedObj in toRemove)

  • originalList.Remove(removedObj);*
    foreach(Object newObj in toAdd)
  • originalList.Add(newObj);*

Ah didn't think of this...only problem is not sure how i could get it to work when I have more than 2 items that require combining [e.g. a,b,c need to be combined so I combine a&b into d but then combine d&c into the final combined item] other than looping over the whole thing with a while loop that only ends when there are no more combinations but that seams extremely brute force and could make things go extremely slow when I may have a large number of items to process.

Trouble is, he's not just removing them, he's combining them (eg. adding their quantities), replacing both with a new summed value.

I see, I didn't notice that you were combining entries. In that case, I still think the easiest solution is to keep two extra lists, one for all the elements you're going to remove and another for the new elements you will add. I'll update my post.

Okay, update's up, ended up having to write more specific protocode than I planned for.

Worked out perfectly Thank you VERY much :D