9

テキスト行の入力ファイルが与えられた場合、重複行を識別して削除したいと考えています。これを実現する C# の簡単なスニペットを示してください。

4

5 に答える 5

39

小さなファイルの場合:

string[] lines = File.ReadAllLines("filename.txt");
File.WriteAllLines("filename.txt", lines.Distinct().ToArray());
于 2009-08-07T15:45:50.143 に答える
22

これでうまくいくはずです(そして、大きなファイルでコピーされます)。

重複する連続した行のみを削除することに注意してください。

a
b
b
c
b
d

として終了します

a
b
c
b
d

どこにも重複したくない場合は、すでに見た一連の行を保持する必要があります。

using System;
using System.IO;

class DeDuper
{
    static void Main(string[] args)
    {
        if (args.Length != 2)
        {
            Console.WriteLine("Usage: DeDuper <input file> <output file>");
            return;
        }
        using (TextReader reader = File.OpenText(args[0]))
        using (TextWriter writer = File.CreateText(args[1]))
        {
            string currentLine;
            string lastLine = null;

            while ((currentLine = reader.ReadLine()) != null)
            {
                if (currentLine != lastLine)
                {
                    writer.WriteLine(currentLine);
                    lastLine = currentLine;
                }
            }
        }
    }
}

これは を前提Encoding.UTF8としており、ファイルを使用することに注意してください。ただし、方法として一般化するのは簡単です。

static void CopyLinesRemovingConsecutiveDupes
    (TextReader reader, TextWriter writer)
{
    string currentLine;
    string lastLine = null;

    while ((currentLine = reader.ReadLine()) != null)
    {
        if (currentLine != lastLine)
        {
            writer.WriteLine(currentLine);
            lastLine = currentLine;
        }
    }
}

(これは何も閉じないことに注意してください - 呼び出し元がそれを行う必要があります。)

連続するものだけでなく、すべての重複を削除するバージョンを次に示します。

static void CopyLinesRemovingAllDupes(TextReader reader, TextWriter writer)
{
    string currentLine;
    HashSet<string> previousLines = new HashSet<string>();

    while ((currentLine = reader.ReadLine()) != null)
    {
        // Add returns true if it was actually added,
        // false if it was already there
        if (previousLines.Add(currentLine))
        {
            writer.WriteLine(currentLine);
        }
    }
}
于 2009-08-07T15:46:52.747 に答える
3

長いファイル(および連続していない重複)の場合は、ファイルを1行ずつコピーして、ハッシュ//位置ルックアップテーブルを作成します。

各行がコピーされるときに、ハッシュ値を確認します。衝突がある場合は、行が同じであることを再確認して、次の行に移動します。((

ただし、かなり大きなファイルの場合にのみ価値があります。

于 2009-08-07T15:51:55.030 に答える
3

すべての一意の文字列をメモリに読み込むよりもオーバーヘッドが少ないストリーミング アプローチを次に示します。

    var sr = new StreamReader(File.OpenRead(@"C:\Temp\in.txt"));
    var sw = new StreamWriter(File.OpenWrite(@"C:\Temp\out.txt"));
    var lines = new HashSet<int>();
    while (!sr.EndOfStream)
    {
        string line = sr.ReadLine();
        int hc = line.GetHashCode();
        if(lines.Contains(hc))
            continue;

        lines.Add(hc);
        sw.WriteLine(line);
    }
    sw.Flush();
    sw.Close();
    sr.Close();
于 2009-08-07T19:12:34.203 に答える
1

私は .net を初めて使用し、より単純なものを作成しましたが、あまり効率的ではない可能性があります。ご意見をお聞かせください。

class Program
{
    static void Main(string[] args)
    {
        string[] emp_names = File.ReadAllLines("D:\\Employee Names.txt");
        List<string> newemp1 = new List<string>();

        for (int i = 0; i < emp_names.Length; i++)
        {
            newemp1.Add(emp_names[i]);  //passing data to newemp1 from emp_names
        }

        for (int i = 0; i < emp_names.Length; i++)
        {
            List<string> temp = new List<string>();
            int duplicate_count = 0;

            for (int j = newemp1.Count - 1; j >= 0; j--)
            {
                if (emp_names[i] != newemp1[j])  //checking for duplicate records
                    temp.Add(newemp1[j]);
                else
                {
                    duplicate_count++;
                    if (duplicate_count == 1)
                        temp.Add(emp_names[i]);
                }
            }
            newemp1 = temp;
        }
        string[] newemp = newemp1.ToArray();  //assigning into a string array
        Array.Sort(newemp);
        File.WriteAllLines("D:\\Employee Names.txt", newemp); //now writing the data to a text file
        Console.ReadLine();
    }
}
于 2016-04-14T19:13:44.797 に答える