Search code examples
c#performancestring-comparison

fastest way to compare two lists of strings in C#


I'm trying to compare two lists of type strings using equal method. Storing the two columns in two different arrays.

Is there any fast method to do the same and also since in two lists the number of entries can be large, so I need to efficient method for doing so.

I need to fetch the two list values from some table. Will an array be good idea to hold the values or some other structure need to use?


Solution

  • If the speed is all your concern you can use Dictionary instead of Lists/Arrays when fetching the data

    // Key (string) - String value
    // Value (int)  - repeat count
    Dictionary<String, int> values = new Dictionary<String, int>();
    
    // Fill values: adding up v1, removing v2
    using (IDataReader reader = myQuery.ExecuteReader()) {
      while (reader.Read()) {
        //TODO: put here the right reader index
        String v1 = reader[1].ReadString();
        String v2 = reader[2].ReadString(); 
    
        int repeatCount;
    
        if (values.TryGetValue(v1, out repeatCount)) 
          values[v1] = repeatCount + 1;
        else 
          values[v1] = 1;
    
        if (values.TryGetValue(v2, out repeatCount))
          values[v2] = repeatCount - 1;
        else 
          values[v2] = -1;
      }
    }
    
    // Select out the keys with positive values (where repeat count > 0)
    List<String> result = values
      .Where(pair => pair.Value > 0)
      .Select(pair => pair.Key)
      .ToList();
    

    However, Linq solution

      List<String> result = List1.Except(List2).ToList();
    

    is much more consize