0

I have a question about deleting duplicates from array list. I want to join two strings from two tables and then split it. Compare the string from one table with others, if duplicates exist then delete it. I did it with this code but i'm asking if there is another faster and shorter way to do it?

table1: String a = "abc"

table2: String b = "def,abc,okl"

Result: "abc,def,okl"

My code:

foreach(DataRow tr in transM.Rows)
{
    foreach(DataRow tb in tableDGV1.Rows)
    {
        if (tr["ID"].ToString() == tb["ID"].ToString())
        {
            string trMitter = tr["Tr"].ToString() + "," + tb["Tr"].ToString();
            string[] trSplit = trMitter.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
            List<string> listTr = new List<string>(trSplit);

            for (int k = 0; k < listTr.Count; k++)
            {
                for (int g = k + 1; g < listTr.Count; g++)
                {
                    if (listTr[g].ToString() == listTr[k].ToString()) listTr.RemoveAt(g);
                }
            }
        }
    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Le Viet Hung
  • 489
  • 4
  • 10
  • 21
  • 2
    I you are using .Net V 3.5 or later, you can simply use `var firstSource = firstString.Split(','); var secondeSource = secondString.Split(',');var result = firstSource.Union(secondSource);` – Steve B Nov 27 '12 at 10:21
  • This question should be moved to [codereview](http://codereview.stackexchange.com/) – MikroDel Nov 27 '12 at 10:22

5 Answers5

2

I you are using .Net V 3.5 or later, you can simply use

var firstSource = firstString.Split(','); 
var secondeSource = secondString.Split(',');
var result = firstSource.Union(secondSource);`

Or use .Distinct():

foreach(DataRow tr in transM.Rows)
{
    foreach(DataRow tb in tableDGV1.Rows)
    {
        if (tr["ID"].ToString() == tb["ID"].ToString())
        {
            string trMitter = tr["Tr"].ToString() + "," + tb["Tr"].ToString();
            string[] trSplit = trMitter.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);

            IEnumerable<string> noDuplicate = trSplit.Distinct();              

        }
    }
}

Or in more Linqish style :

var noDuplicate = from DataRow row1 in transM.Rows
                  from DataRow row2 in tableDGV1.Rows
                  where row1["ID"].Equals(row2["ID"])
                  let array1 = row1["tr"].ToString().Split(',', StringSplitOptions.RemoveEmptyEntries)
                  let array2 = row2["tr"].ToString().Split(',', StringSplitOptions.RemoveEmptyEntries)
                  select array1.Union(array2).Distinct();
Steve B
  • 36,818
  • 21
  • 101
  • 174
0

I suspect that the big performance hit is on the loops on the table rows.
You could optimize your query using this (if the ID column is the primary key)

foreach(DataRow tr in transM.Rows)
{
    string idValue = tr["ID"].ToString();
    DataRow[] foundRows = tableDGV1.Select("ID = " + idValue);
    if(foundRows.Length == 1)
    {
       // Here you have the internal row to join with the external one
        string joinedString = tr["Tr"].ToString() + "," + foundRows[0]["Tr"].ToString();
        string[] trSplit = joinedString.Split(new char[] {','}, 
                           StringSplitOptions.RemoveEmptyEntries);
        List<string> listTr = trSplit.Distinct().ToList();                        
        .........
    }
}

this will remove the unnecessary loops on the internal table

Steve
  • 213,761
  • 22
  • 232
  • 286
0

You can create first a list with all the elements, sort the list and remove the duplicates. There are a related question here: Remove duplicates from array

Community
  • 1
  • 1
slamora
  • 697
  • 10
  • 17
0

Enumerable.Union() method removes duplicates:

var items1 = new[] { "abc" };
var items2 =  "def,abc,okl";

var splitItems2 = items2.Split(',');

var result = 
    items1.Union(splitItems2) // Removes duplicates
    .ToArray();
maximpa
  • 1,958
  • 13
  • 16
0

You can use: Enumerable.Union which produces the set union of two sequences by using the default equality comparer.

        string[] id1 = { "abc" };
        string[] id2 = { "def", "abc", "okl" };

        IEnumerable<string> both = id2.Union(id1);

        foreach (string id in both)
            Console.WriteLine(id);
03Usr
  • 3,335
  • 6
  • 37
  • 63