This problem is based around duplicates AND their position. There are many ways to remove duplicates but the position factor changes things.
You will have to have some form of loop or aggregate function:
const string input = @"a a a and the best way to write write a paper is to buy a research paper.
the the best place to buy buy a paper is to go go online."
string RemoveDuplicateWords(){
List<string> resultRows = new List<string>();
string[] rows = input.split('\n');
foreach(var row in rows){
resultRows.Add(row.Split(' ').Aggregate((current,next)=>current.Split(' ').Last==next?current:current+' '+next));
}
return string.Join("\n",resultRows);
}
</string></string>
Some issues:
This does have some grammatical issues where two words may, and should appear together:
My Brother said that he hadn't had anything to eat, but in fact he had had dinner. <-correct
It is true that that that that that man used in that sentence is wrong. <- also correct The first and fourth (and sixth) 'that's are relative pronouns, the second and fifth 'that's are adjectives and the third 'that' is a noun. All 'that's in the previous sentence are nouns.
It can be written better:
It is true that that 'that' that that man used in that sentence is wrong. <- '' is allowed to clarify uncommon or uncertain nouns. The other 'that's stand as we have to assume that several men used several 'that's and we are pointing out the incorrect one.
Now: If anyone points out a use of the word 'that' that I have used incorrectly, this post might become so recursively meta that the universe will collapse in on itself.
YOU HAVE BEEN WARNED!
UPDATE:
And also in VB #^_^#
Converted with
http://codeconverter.sharpdevelop.net/SnippetConverter.aspx[
^]
Const input As String = "a a a and the best way to write write a paper is to buy a research paper." & vbCr & vbLf & " " & vbCr & vbLf & "the the best place to buy buy a paper is to go go online."
Public Function RemoveDuplicateWords() As String
Dim resultRows As New List(Of String)()
Dim rows As String() = input.Split(ControlChars.Lf)
For Each row As var In rows
resultRows.Add(row.Split(" "C).Aggregate(Function(current, [next]) If(current.Split(" "C).Last = [next], current, current + " "C + [next])))
Next
Return String.Join(vbLf, resultRows)
End Function