-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.

4
  • 1
    I don't see any duplicate words in your sentence. Do you mean duplicate patterns? Commented Feb 19, 2017 at 7:23
  • And even with patterns, why is the i in "programming" not removed (it appears earlier in the string)? Commented Feb 19, 2017 at 7:24
  • I was thinking of the "mm", but yeah. Is there a minimum number of characters that need to match? Commented Feb 19, 2017 at 7: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. Commented Feb 19, 2017 at 7:35

4 Answers 4

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).

Sign up to request clarification or add additional context in comments.

5 Comments

That's technically correct but not especially helpful.
@DM some solution explorations are just the wrong way to go. if you understand the reasoning for not doing it, why do it?
I agree with this train of thought.
Thank you. I'm very new to java and this is a question issued by my teacher as a challenge.
Surely the question is about direct, rather than indirect use of arrays. String could be a completely opaque class that doesn't use arrays.
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);
    }
}

Comments

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);
        }

}

1 Comment

You should update your existing answer rather than add another
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);
    }

}

3 Comments

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"
Because, First 5 letters of Progress - "Progr" is the duplicate of first five letters of "Programming". Hence removed.
In case you want to handle those kind of stuation as well. Below is the updated code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.