-3

So I have a time limited problem (the subroutine should be as fast as possible) for doing this. At any point of file I have 2 network files, like this:

Time: 23.369
1464953512
Bytes: 4193304
Time: 24.369
1464953502
Bytes: 4194304

Time: 24.369
1464953502
Bytes: 4194304
Time: 25.404
1464953527
Bytes: 4194304

Take note the 2 files are different and each contain a subset of information, and I need to merge these 2 to create the cumulative information(removing duplicates), like this:

Time: 23.369
1464953512
Bytes: 4193304
Time: 24.369
1464953502
Bytes: 4194304
Time: 25.404
1464953527
Bytes: 4194304

What is the fastest way to do this? (with some code please, if possible).

Thanks.

P.S. I was looking at some diff/merge libraries but I think it will be a overkill for the same. Any simple .net/LINQ magic that can achieve it? Also, the duplicates are serial, as shown and not scattered around.

EDIT: -ve voters please leave a comment so that I can improve or otherwise change the question to be more suitable.

Jishan
  • 1,654
  • 4
  • 28
  • 62

2 Answers2

0

If this helps anybody, I used this to find the union of 2 text files, having converted them into string enum before:

var dinfo = new DirectoryInfo(@"C:\http");
var files = dinfo.GetFiles("*.txt");
IEnumerable<string> _eValA = null;
IEnumerable<string> _eValB = null;

_eValA = File.ReadLines(@"C:\http\http1.txt");
_eValB = File.ReadLines(@"C:\http2.txt");

IEnumerable<String> union = _eValA.Union(_eValB);

//TODO: create file if does not exist
File.WriteAllLines(@"C:\http\union.txt", union.Cast<String>()); 
Jishan
  • 1,654
  • 4
  • 28
  • 62
0

You'll want to read in the files, creating instances of a custom class as you go with two properties: Time and Bytes. In your custom class, override the Equals and GetHashCode methods and have them use the Time property. For example:

public override int GetHashCode() {
    return Time.GetHashCode();
}

public override bool Equals(obj other) {
    //skipping type check and null check for brevity
    return Time.Equals(other.Time);
}

Then just add your items to a HashSet<YourCustomClass>. HashSet doesn't allow duplicates, so you'll be good to go.

Spivonious
  • 718
  • 7
  • 20