我有一个包含许多重复项的文本文件,并希望删除重复项,然后以相同的顺序输出文件的更新版本,例如:
原始
10
a
f
a
b
g
a
f
b
h
r
更新版本
a
f
b
g
h
r
我想使用某种类型的数组实现这一点,这是我到目前为止所拥有的,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Duplicates
{
public partial class Form1 : Form
{
//Global Variables
int[] Original;
public Form1()
{
InitializeComponent();
}
//Exit Application
private void mnuExit_Click_1(object sender, EventArgs e)
{
this.Close();
}
//Load File
private void mnuLoad_Click_1(object sender, EventArgs e)
{
//code to load the numbers from a file
OpenFileDialog fd = new OpenFileDialog();
//open the file dialog and check if a file was selected
if (fd.ShowDialog() == DialogResult.OK)
{
//open file to read
StreamReader sr = new StreamReader(fd.OpenFile());
int Records = int.Parse(sr.ReadLine());
//Assign Array Sizes
Original = new int[Records];
//Go through text file
for (int i = 0; i < Records; i++)
{
Original[i] = int.Parse(sr.ReadLine());
}
}
}
private void btnOutput_Click(object sender, EventArgs e)
{
//store Original array
string Output = "Original \n";
for (int i = 0; i < Original.Length; i++)
{
Output = Output + Original[i] + "\n";
}
int[] TempArray = new int[Original.Length];
for (int i = 0; i < Original.Length; i++)
{
TempArray[i] = Original[i];
}
//add code here
//output the original array and new array
Output = Output + "Original with Delete\n";
for (int i = 0; i < Original.Length; i++)
{
Output = Output + Original[i] + "\n";
}
lblOutput.Text = Output;
}
}
}
我也在使用Windows窗体应用程序
答案 0 :(得分:4)
使用LINQ Distinct方法删除重复项:
TempArray = Original.Distinct().ToArray();
答案 1 :(得分:0)
//load and output
private void mnuLoad_Click_1(object sender, EventArgs e)
{
if (fd.ShowDialog() == DialogResult.OK)
{
//open file to read
StreamReader sr = new StreamReader(fd.OpenFile());
//skip first line;
sr.ReadLine();
string s;
//Remove duplications
Dictionary<string, int> unique_lines = new Dictionary<string, int>();
for (int i = 0;
(s = sr.ReadLine()) != null; //read until reach end of file
i++)
if(!unique_lines.Keys.Contains(s))
unique_lines[s] = i;//save order of line
//Restore order:
SortedDictionary<int, string> sorted_lines = new SortedDictionary<int, string>();
foreach (string key in unique_lines.Keys)
sorted_lines[unique_lines[key]] = key;
//Output:
string output = string.Empty;
foreach (int key in sorted_lines.Keys)
output += sorted_lines[key] + "\n";
lblOutput.Text = output;
}
}