How can I delete elements in a list with the same values ​specified in square brackets?

Viewed 52

I have a list and I indexed them myself using "[","]".

As you can see below, some of these elements are duplicated, but their values ​​before the index in square brackets are different.

How can I delete duplicated items from the indexes that I have specified?

6557.94172139931[6]
5354.8168281469[9]
5354.81740416957[9]
5760.29235605679[8]
6212.12135703147[7]
4869.7862090936[95]
4869.78639031275[95]
3681.97068695995[10]
3681.9727736492[10]

What i want:

6557.42618426901[6]
5354.8168281469[9]
5760.29235605679[8]
6212.12135703147[7]
4869.7862090936[95]
3681.97068695995[10]

Here is my code that I'm trying to do:

IEnumerable<string> matchingList;
foreach (string item in distanceandlayername)
{
    string index = item.Split('[', ']')[1];
    matchingList = distanceandlayername.Where(x => x.Contains($"[{index}]"));

    foreach (string part in matchingList)
    {
        int i = distanceandlayername.IndexOf(index);
        distanceandlayername.RemoveAt(i);
    }
2 Answers

Something like this might work.

for(var i = 0; i < distanceandlayername.Count; i++)
{ 
    var item = distanceandlayername[i];
    string layer = item.Substring(item.IndexOf('['));   
    distanceandlayername.RemoveAll(x=> x!= item && x.Contains(layer));
}

If you also might have duplicate distances(so complete identical items) this needs to be adapted.

Update

Changes after the revised spec ("please sort it"). I now convert the original line into a (double, int) tuple and work with that. That allows things to be sorted.

In the end, I format things the way you do originally. As @ralf says, you should consider having something that not just one weird string.

First create your list of numbers (by the way, this code should be in your question):

private static readonly List<string> annotatedNumbers = new List<string>
{
    "6557.94172139931[6]",
    "5354.8168281469[9]",
    "5354.81740416957[9]",
    "5760.29235605679[8]",
    "6212.12135703147[7]",
    "4869.7862090936[95]",
    "4869.78639031275[95]",
    "3681.97068695995[10]",
    "3681.9727736492[10]",
};

Then write some code like this. It accumulates the indexes to remove and then, in a second pass, removes those indexes:

public static IEnumerable<(double distance, int layer)> GetTrimmedList(List<string> withDups)
{
    var layerNumbersToCheck = new List<int>();
    var distanceAndLayerNames = new List<(double distance, int layer)>();

    foreach (var item in withDups)
    {
        var parts = item.Split('[', ']');
        if (parts.Length < 2)
        {
            throw new ArgumentException($"Item: {item} incorrectly formatted");
        }
        if (!double.TryParse(parts[0], out var distance))
        {
            throw new ArgumentException($"Distance in item: {item} is not a valid double");
        }
        if (!int.TryParse(parts[1], out var layerNum))
        {
            throw new ArgumentException($"Layer number in item: {item} is not a number");
        }


        if (!layerNumbersToCheck.Contains(layerNum))
        {
            layerNumbersToCheck.Add(layerNum);
            distanceAndLayerNames.Add((distance, layerNum));
        }
    }

    var result = distanceAndLayerNames.OrderBy(item => item.layer);

    return result;
}

}

Finally, some code to exercise the function:

public static void Test()
{
    var result = GetTrimmedList(annotatedNumbers);
    foreach(var item in result)
    {
        Console.WriteLine($"{item.distance}[{item.layer}]");
    }
}

The result looks like:

6557.94172139931[6]
6212.12135703147[7]
5760.29235605679[8]
5354.8168281469[9]
3681.97068695995[10]
4869.7862090936[95]
Related