
Ehtasham T.
asked 08/19/19write a program to remove duplicate string from a sentence without using any predefined method. Example : input :: abc hello is my abc is xyz output:: abc hello is my xyz
1 Expert Answer

Patrick B. answered 08/19/19
Math and computer tutor/teacher
class Etasham
{
public String RemoveMultipleStrings ( String inbuff)
{
String [] tokens = inbuff.split(" "); //splits the sentence into the array of string tokens
int N = tokens.length;
String [] dictionary = new String[N]; //sets up the dictionary of words already used
int iIndex=-1;
for (int iLoop=0; iLoop<N; iLoop++) { dictionary[iLoop]=null; }
String outbuff = " "; //initalizes the output buffer
for (int iLoop=0; iLoop<N; iLoop++) //for each string in the tokens array
{
//is the current token in the dictionary???
//DEBUG: System.out.println(" current token is >"+tokens[iLoop]+"<");
if (LinearSearch(dictionary,tokens[iLoop])>=0)
{
// YES , it is... so ignore it because it has already been used
}
else
{
// No it is not...
outbuff = outbuff + tokens[iLoop] + " " ; // writes the token to the output buffer
//could also pass the iIndex here instead of checking if the item in the dictionary is null
dictionary[++iIndex]= new String(tokens[iLoop].toUpperCase()); //adds the token to the dictionary
}
}
return(outbuff.trim());
}
public int LinearSearch( String [] A, String target)
{
int iReturn = -1;
for (int iLoop=0; iLoop<A.length; iLoop++)
{
if (A[iLoop]==null) { break; }
if (A[iLoop].equalsIgnoreCase(target.toUpperCase()))
{
iReturn = iLoop;
break;
}
}
return(iReturn);
}
public static void main( String args[])
{
Etasham X = new Etasham();
String inbuff = "abc hello is my abc is xyz";
System.out.println(X.RemoveMultipleStrings(inbuff));
inbuff = "one one two one two three one two three four one two three four five " +
" one two three four five six one two three four five six seven " +
" one two three four five six seven eight " +
" one two three four five six seven eight nine " +
" one two three four five six seven eight nine zero ";
System.out.println(X.RemoveMultipleStrings(inbuff));
}
} //class
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
uploaded into RESOURCES section under TOOLKIT. You must rename it to *.java file so as to compile and run08/19/19