title: C# Efficient de-duplication of single Datatable column's data
body: <p>I have a Datatable with some data, example as below, and need to de-duplicate any names in the names field by appending [1], [2] etc.</p>
  
  <p>Current code below, works but is slow on large tables.
  Any tips on the most efficient way of doing this in C# would be appreciated.</p>
  
  <p>Current Table sample:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>- ID Name  X Y
  - 1 John  45 66
  - 2 Paul  44 66
  - 3 George  88 102
  - 4 John  33 90
  - 5 John  53 37
  - 6 Paul  97 65
  - 7 Ringo  01 87
  - 8 Ringo  76 65​</code></pre>
  </div>
  </div>
  </p>
  
  <p>Required Table sample:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>- ID Name  X Y
  - 1 John[1]  45 66
  - 2 Paul[1]  44 66
  - 3 George  88 102
  - 4 John[2]  33 90
  - 5 John[3]  53 37
  - 6 Paul[2]  97 65
  - 7 Ringo[1] 01 87
  - 8 Ringo[2] 76 65​</code></pre>
  </div>
  </div>
  </p>
  
  <p>Current code below:</p>
  
  <pre><code>foreach (DataRow aRow in ds.Tables[0].Rows) // run through all
     {
     string aName = aRow["Name"].ToString();                
     DataRow[] FoundRows = ds.Tables[0].Select("Name = '" + aName +"'"); // Find all rows with same name
  
      if (FoundRows.Length &gt; 1) // As will always find itself
         {
            int i = 1;
            foreach (DataRow row in FoundRows)
              {
               row["Name"] = row["Name"].ToString() + "[" + i + "]";
                i++;
              }
          ds.Tables[0].AcceptChanges(); // Ensure the rows are updated before looping around.
          }
      }
  </code></pre>