Link to home
Start Free TrialLog in
Avatar of CipherIS
CipherISFlag for United States of America

asked on

C# remove duplicates from list where there is more than one repeating char in the item.

I want to remove duplicates.  Using the below example I want "Alpha" and "Gamma" removed from the list because "Alpha" has 2 A's and "Gamma" has two A's and two M's.

List<string> theList = new List<string>() { "Alpha", "Beta", "Gamma", "Delta" };

The values that should be returned should be distinct in each item.  No duplicate letters.
Avatar of Jeff Edmunds
Jeff Edmunds
Flag of United States of America image

You could iterate through each string in the list...

With each individual string (like "Alpha") you could use the string's ToCharArray method to separate each character in the string.

Create a new List of Char objects that will be your test area... Step through each Char in the array and, just before adding it to the new list, use the list's Contains(T) method to see if the Char already exists. If it doesn't, add the character to the list and move on to the next character. If it does exist, you know you can remove the string from the original list, using the Remove(T) method and move on to the next string in the original list.
SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CipherIS

ASKER

I tried to implement your suggestion but it doesn't work with what i'm actually working with.  Thought if I gave a generic example it would work.  Working on an app for a trader.  Using permutations to build a list.  Below is an example.
    public class ValuesModel
    {
        public string V01 { get; set; }
        public string V02 { get; set; }
        public string V03 { get; set; }
        public string V04 { get; set; }
        public string V05 { get; set; }
    }

    List<ValuesModel> values = new List<ValuesModel>();
    values.Add(new ValuesModel() 
    {
        V01 = 1, V02 = 2, V03 = 3, V04 = 4, V05 = 5,
    });
    values.Add(new ValuesModel() 
    {
        V01 = 1, V02 = 1, V03 = 3, V04 = 4, V05 = 5,
    });
    values.Add(new ValuesModel() 
    {
        V01 = 5, V02 = 4, V03 = 3, V04 = 3, V05 = 1,
    });

Open in new window

Using above example of the three entries the only valid item should be
1, 2, 3 4, 5.

1,1,3,4,5 need to be eliminated because 1 exists more than 1 x.

5,4,3,2,1 needs to be eliminated because it has same combination as 1,2,3,4,5 regardless of order.

Thanks
Saige,

Thanks for the input.  I did get this to work.  I'll try to post later to see if there is a way to make it more efficient.

Thanks
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hope this problem is resolved by now.