0

I am trying to display duplicate string from an array but the code is not working.For example:In the word "arrow" r is coming two times shoq the code should display the output 'r'.But it is not displaying anything

public class duplicatearray {
        public static void main(String[] args)
        {
            String[] s={"arrow"};
            for(int i=0;i<s.length;i++)
            {
                for(int j=i+1;j<s.length;j++)
                {
                    if(s[i].equals(s[j]))
                    {
                        System.out.println("Duplicate value"+s[i]);
                    }
                }
            }

        }
    }
6
  • You are checking against String Array. Do you need this array? or you need only one string. Commented Feb 8, 2018 at 6:22
  • Your question title and description have no match. And I edited your title after reading the description. Feel free to revert it back if that makes no sense. Commented Feb 8, 2018 at 6:28
  • @PrathibhaChiranthana From the example, I guess in a String, duplicate char's Commented Feb 8, 2018 at 6:32
  • @ꜱᴜʀᴇꜱʜᴀᴛᴛᴀ String[] s={"arrow"}; was confused. Commented Feb 8, 2018 at 6:35
  • @PrathibhaChiranthana What so tricky in it ? It is just a String array with only one element Commented Feb 8, 2018 at 6:38

4 Answers 4

1

For example:In the word "arrow" r is coming two times shoq the code should display the output 'r'.

You are checking your checking two Strings. Where as you want to check for duplicate characters

if(s[i].equals(s[j]))

It should be

if(s[i].charAt(j) == s[i].charAt(j+1))

Then it check against characters in the same Strings. And also in the print statement

System.out.println("Duplicate value"+s[i].charAt(j));

And you loop also wrong, you should start at 0'th character

for(int j= 0 ;j<s[i].length() -1 ;j++)

So overall it should be

String[] s = { "arrow" };
for (int i = 0; i < s.length; i++) {
    for (int j = 0; j < s[i].length() - 1; j++) {
        if (s[i].charAt(j) == s[i].charAt(j + 1)) {
            System.out.println("Duplicate value : " + s[i].charAt(j));
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I am trying to display duplicate string from an array, I think the question clearly states otherwise
@AbSin Right. The Question and the description have mismatched.
Yeah I notice too, shouldn't have doubted you.
@AbSin No problem. The title and description are messy anyway :D
0

Try with below code snippet It will give you what is expected.

    public static void main(String[] args) {

            String[] s = {"arrow"};
            for (int k =0; k < s.length; k++) {

            char[] arr = s[k].toCharArray();//get char array

            for (int i = 0; i < arr.length; i++) // 1
            {
                int duplicateCount = 0;
                for (int j = 1; j < arr.length; j++) {
                    if (arr[i] == (arr[j])) {
                        duplicateCount++;//duplicate variable
                        if (duplicateCount >= 2)
                            System.out.println("Duplicate value " + arr[i]);
                    }
                }
            }
        }
    }

Comments

0

I see a flaw in your code.

String[] s={"arrow"};

you need not declare this as an array. Just a string is suffice.If you call s[i] on this, it will always return "arrow" not 'a' or 'r' as you are expecting. You had to have a char[] for it.

Anyhow, i used your input in my code and printed out the necessary statements. Go through it. Hope it helps.

Try with HashSet. It gives better performance.

import java.util.Collection;
import java.util.HashSet;


public class duplicate {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String[] s={"arrow"};
        System.out.println(s[0]);
        HashSet<Character> hash = new HashSet<Character>();
        char[] s1 = s[0].toCharArray();
        for(char c : s1){
            if(!(hash.add(c)))
                System.out.println(c);
        }

    }

}

Comments

0

your code is using two for loops which can take more time as string size increases. try with below code snippet

public class DuplicateArray {
    public static void main (String...args) {
        String str = new String("arrow");
        int[] dupArray = new int[256];
        for (int i=0;i<str.length();i++) {
            char ch = str.charAt(i);
            if (dupArray[ch] == 0) {
                dupArray[ch]++;
            } else {
                System.out.println("Duplicate value : "+ch+" ");
            }
        }
    }
}

Comments

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.