How to remove same combination (same number but diffrent order) on list?

James DEV

I have a string List like this

code

    List<string> MyList = new List<string>();
    MyList.Add("153");
    MyList.Add("112");
    MyList.Add("531");
    MyList.Add("675");
    MyList.Add("889");
    MyList.Add("351");

code
I want only one combination of (153) or (531) or (351).
The output should be
153
112
675
889

Edit:I know if I want to remove duplicate i should use MyList =MyList.Distinct(); as suggested in another duplicate Remove duplicates from a List<T> in C# of my previous (now deleted) question. but the problem is (153),(531),(351) are not duplicate.

Obviously I read documentation and it shows some alternative version of Distinct with some sort of "comparer", but example there is for objects and I have strings.

When I asked the same question about an hour ago it was closed by ... as duplicate of https://stackoverflow.com/a/50177/477420 which clearly not applicable to me at all - I don't know why I would ever compare two strings as sequences of characters ignoring order. I still tried that code and it returned "true" for one of the cases I have:

"153".OrderBy(i => i).SequenceEqual("531".OrderBy(i => i))

Even then it does not look useful.

quain

A quick and a bit dirty way of getting what you wanted, without custom comparer and with items in original order:

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
        List<string> MyList = new List<string>();
        MyList.Add("153");
        MyList.Add("112");
        MyList.Add("531");
        MyList.Add("675");
        MyList.Add("889");
        MyList.Add("351");
        
        int i = 0;
        var ac = new Dictionary<int, string>();
        var res = new Dictionary<int, string>(); 

        foreach (var a in MyList)
        {
            res.Add(i, a);
            var ou = a.ToArray();
            Array.Sort(ou);
            ac.Add(i, new String(ou));
            i++;
        }

        var filteredDictionary = ac.GroupBy(s => s.Value).Where(gr => gr.Any()).Select(g => g.First()).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
        var ret = res.Where(x => filteredDictionary.ContainsKey(x.Key)).Select(x => x.Value);
        foreach (var c in ret)
        {
            Console.WriteLine(c);
        }
    }
}

Result:

153
112
675
889

Fiddle: https://dotnetfiddle.net/#&togetherjs=1dgVkP8AGf

This does not contain any safety checks, nor does it handle strings that are not 'numbers', but will work with your example leaving first occurrence of given combination.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to remove the same pair with diffrent order in values inside dictionary python?

Remove Multiple Record With Same Order Number

how to remove same number in php?

How can I remove nested list items from a python list if they have the same order, but start at a different index

How to remove all combination with the same element in subarray from array?

How to find rank of order number by same start

import same random number in diffrent classes for diff variable

How to paste two list columns with the same order?

How do you remove duplicates in a 2d list with the same values but different order

Python List comprehension Remove same tuples but different order

How can i remove same item in list

How to remove same list from matrix by python

How to remove from a series of list the same column

How to count the number of orders and the number of returns from same order id?

How to fetch list of objects with same phone number

How to find number of same elements in list Prolog?

How to list a number of files with the same ending?

How to add the data in the same row but in diffrent columns according to the hotel names

Compile same source with diffrent aliases

SQL ORDER BY - how to honour same order as list in WHEN clause

Remove number from list if the first digit and length are the same

How would I check if a list is in another list in the same order?

How can I verify that a list elements are in same order of another list or not?

How to search in a list whether a list of given items exists in the same order

How to check if list contains another list in same order

How to remove an item from 2 array's with the same order?

How to order query by number first and then by string in the same field

How to order array of objects by the number of the keys the same as the given ones as a parameter?

How to count the number of same elements which are in order in a vector in R?