-1

Is there any way we can remove duplicate words from a String without using Arrays?

For example, I have this sentence "this is java programming program", and the output have to be "this java programming".

I see similar remove duplicate problems but all of them are using arrays.

Han
  • 13
  • 1
  • 3
  • 1
    I don't see any duplicate words in your sentence. Do you mean duplicate patterns? – Kyle Martin Feb 19 '17 at 07:23
  • And even with patterns, why is the `i` in "programming" not removed (it appears earlier in the string)? – Henry Feb 19 '17 at 07:24
  • I was thinking of the "mm", but yeah. Is there a minimum number of characters that need to match? – D M Feb 19 '17 at 07:27
  • well, it is supposed to be words and substrings. In "this is", the "is" substring of "this". Same with "program" in "programming". I am supposed to check words separated by space. – Han Feb 19 '17 at 07:35

4 Answers4

2

well, in Java, Strings are actually objects wrappers for character arrays which primarily add immutability.

so, there is no actual way to not use arrays for your task. even if you wrap your head around a solution which doesn't use any direct array creation in the code implementation, you will still be using arrays behind the scenes (if you want your solution to be optimal).

omerkarj
  • 663
  • 2
  • 7
  • 18
1

Remove duplicate words from a given string using simple way

package inffrd;

public class Q001 
{
    public static void removeduplicate(String input)
    {
        //convert the string to array by splitting it in words where space comes
        String[] words=input.split(" ");
        //put a for loop for outer comparison where words will start from "i" string
        for(int i=0;i<words.length;i++)
        {
            //check if already duplicate word is replaced with null
            if(words[i]!=null)
            {
                //put a for loop to compare outer words at i with inner word at j
                for(int j =i+1;j<words.length;j++)
                {
                    //if there is any duplicate word then make is Null
                    if(words[i].equals(words[j]))
                    {
                        words[j]=null;
                    }
                }
            }
        }
        //Print the output string where duplicate has been replaced with null
        for(int k=0;k<words.length;k++)
        {
            //check if word is a null then don't print it
            if(words[k]!=null)
            {
                System.out.print(words[k]+" ");
            }
        }
    }

    public static void main(String[] args) 
    {
        String s1="i am dinesh i am kumar";
        Q001.removeduplicate(s1);
    }
}
Daniel Puiu
  • 962
  • 6
  • 21
  • 29
RANA DINESH
  • 131
  • 5
0

Below code will help you :)

public class RemDup
{
    public static void main ( String[] args )
    {
        String sentence = "this is java programming program";
        int max_word_length = sentence.length()/2;
        int min_word_length = 2;
        while(max_word_length>=min_word_length)
        {
        int si = 0;
        int ei = max_word_length;
        while ( ei<sentence.length() )
        {
            int e=ei;
            while ( e<sentence.length() )
            {
                int ind = sentence.indexOf ( sentence.substring ( si, ei ),e );
                if ( ind!=-1 )
                {
                    sentence = sentence.substring ( 0,ind ) +sentence.substring ( ind+max_word_length,sentence.length() );
                    e=ind+max_word_length;
                }
                else break;
            }


            si+=1;
            ei+=1;

        }
        max_word_length--;
        }
        System.out.println(sentence);
    }

}
  • Hi, It was ok for the "this is java programming program". But if we put longer sentences like "this is java programming program progress", it would output "this java programmingess" – Han Feb 19 '17 at 09:58
  • Because, First 5 letters of Progress - "Progr" is the duplicate of first five letters of "Programming". Hence removed. –  Feb 19 '17 at 10:02
  • In case you want to handle those kind of stuation as well. Below is the updated code –  Feb 19 '17 at 10:32
0

Below is the updated code @Han

public class RemDup
{
        public static void main ( String[] args )
        {
            String sentence = "this is java programming program progress";
            int max_word_length = sentence.length()/2;
            int min_word_length = 2;
            while(max_word_length>=min_word_length)
            {
            int si = 0;
            int ei = max_word_length;
            while ( ei<sentence.length() )
            {
                int e=ei;
                while ( e<sentence.length() )
                {
                    int ind = sentence.indexOf ( sentence.substring ( si, ei ),e );
                    if ( ind!=-1 )
                    {
                        if(
                         sentence.substring(ind-1,ind).equals(" ")
                        &((ind+max_word_length)>=sentence.length()||
                        sentence.substring(ind+max_word_length,ind+max_word_length+1).equals(" "))
                        )
                        {
                        sentence = sentence.substring ( 0,ind ) +sentence.substring ( ind+max_word_length,sentence.length() );
                        }
                        e=ind+max_word_length;

                    }
                    else break;
                }


                si+=1;
                ei+=1;

            }
            max_word_length--;
            }
            System.out.println(sentence);
        }

}