Submitted by smitty239 on Thu, 08/03/2017 - 01:52
Forums:
I have a text file, on each line of this text file is a components name, I need to delete all duplicate entries from this text file. does anyone have code to do this? or can anyone help? there could be up to 2000 component names in the file
re: delete duplicate lines of text
Create a list object (of type string) to hold the unique values from the text file then read the file line by line and use the .Contains method of the list to see if it already has the value; if it does not contain the value, .Add it to the list. The result will be a list of the unique values from your text file. Now write these values to a file (either overwrite the existing file or create a new one).
Two ways
The easiest way would be to open the file in Excel, and use the "Remove Duplicates" function on the "Data" tab.
If you really want to write code, use a System.Collections.Generic.HashSet. Just keep adding items to the HashSet. Items that are already members won't be repeated.