1

How to print only the arraylist values without repeating the highlighted text part

referred the solutions provided but looks complex, please provide a simple solution on how to print only the values

import java.util.Arrays;

public class DuplicateNuminArray {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String progLanguages[] = {"Java", "Python", "C", "C++", "Java", "C"};


        for (int i = 0; i < progLanguages.length; i++) {
            for (int j = i+1; j < progLanguages.length; j++)
            {
                if(progLanguages[i].equals(progLanguages[j]))
                {   
                    System.out.println("The Duplicate string is :" +progLanguages[i]);

                }
            }

        }

    }

I expect the print statemenet to print only as below The Duplicate string is :Java, C
instead of The Duplicate string is :Java The Duplicate string is :C

Pie
  • 584
  • 1
  • 6
  • 22
  • I expect the print statement to print only as below The Duplicate string is :Java, C instead of The Duplicate string is :Java The Duplicate string is :C –  May 18 '19 at 03:09

2 Answers2

2

You would need to collect all of the duplicates in a data-structure (and preserve uniqueness). One such data-structure is the collection type Set. Something like,

String progLanguages[] = { "Java", "Python", "C", "C++", "Java", "C" };
Set<String> set = new LinkedHashSet<>();
for (int i = 0; i < progLanguages.length; i++) {
    for (int j = i + 1; j < progLanguages.length; j++) {
        if (progLanguages[i].equals(progLanguages[j])) {
            set.add(progLanguages[i]);
        }
    }
}
System.out.printf("The Duplicate string is: %s%n", set);
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Thanks a lot Elliott, this address my question, you've provided very good solution. Thanks a lot for your swift response. Appreciate your help!! –  May 18 '19 at 03:18
0

I like Elliott's answer better, but an alternative answer is to use print instead of println. If you just move one print statement outside of the for loops and then just use print in side your if block instead of println you'll just append on the end all in line. Like so:

 String[] progLanguages = {"Java", "Python", "C", "C++", "Java", "C"};

        System.out.print("The Duplicate string is: ");
        for (int i = 0; i < progLanguages.length; i++) {
            for (int j = i+1; j < progLanguages.length; j++)
            {
                if(progLanguages[i].equals(progLanguages[j]))
                {
                    System.out.print(progLanguages[i] + " ");

                }
            }

        }

Or even look into StringBuilder

 StringBuilder myBuilder = new StringBuilder("The Duplicate String is: ");
        for (int i = 0; i < progLanguages.length; i++) {
        for (int j = i+1; j < progLanguages.length; j++)
        {
            if(progLanguages[i].equals(progLanguages[j]))
            {
                myBuilder.append(progLanguages[i]);
                myBuilder.append(" ");
            }
        }

    }
        System.out.println(myBuilder.toString());
chas spenlau
  • 325
  • 1
  • 2
  • 12
  • Thanks a lot Chas, this also helps me to solve this problem in different ways. Thank you for detail explanation. –  May 18 '19 at 04:13