1

I want to print duplicate and non duplicate values separately.

String[] values= { "Java","C", "JavaScript", "JavaScript", "Java" ,"Java", "Java" , "JavaScript", "Java", "Ruby"};

My output should be below.

String[] duplicate = {"Java", "Java", "Java", "Java", "Java", "JavaScript", "JavaScript", "Java"}

String[] unique = {"C", "Ruby"} 

I have tried several ways but I couldn't able to make it.

public static void main(String[] args) {

        String[] a = { "Java","C", "JavaScript", "JavaScript", "Java" ,"Java", "Java" , "JavaScript", "Java", "Ruby"};

         List<String> uniq = new ArrayList<String>();
         List<String> dupe = new ArrayList<String>();

        int count = 0;
        for (int j = 0; j < a.length; j++) {
            for (int k = j + 1; k < a.length; k++) {
                if (a[j] == a[k]) {
                    count++;
                }
            }
            if (count == 1){
                System.out.println("Dupe: "+a[j]);
                dupe.add(a[j]);
            } 
            count = 0;

        }
    }

Output


String[] Duplicate = {"Java", "Java", "Java", "Java", "Java", "JavaScript", "JavaScript"}

String[] Unique = {"C", "Ruby"}
NJY404
  • 349
  • 3
  • 14
User
  • 57
  • 1
  • 7

6 Answers6

1

You can do it in one iteration by using Collectors.partitioningBy but the return type will be Map<Boolean, List<String>>

List<String> l = Arrays.asList(values);
Map<Boolean, List<String>> d = Arrays.stream(values)
             .collect(Collectors.partitioningBy(i->Collections.frequency(l, i)>1));

Output List associated with true key are in duplicate array, were other are not in duplicate array

{false=[C, Ruby], true=[Java, JavaScript, JavaScript, Java, Java, Java, JavaScript, Java]}
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
0

This answer might be helpful to you.

It says that you can use a set to create a collection of the input array's unique values like:

int length= a.length;
Set<String> set = new HashSet<String>();

for(int i = 0; i < length; i++){
  set.add(a[i]);
}

You can then determine the duplicate values by creating an ArrayList from the original array a and removing all the unique values from the new ArrayList like:

ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(a));
arrayList.removeAll(set)

The end result of both of these commands is that arrayList contains only the duplicate values that a originally had, and set contains only a's unique values.

AbsoluteSpace
  • 710
  • 2
  • 11
  • 21
0

The below code will give you a Map with the strings and the number of times it appears:

import java.util.HashMap;
import java.util.Map;

public class TestSample {
    public static void main(String[] args) {
        String[] values =
                { "Java", "C", "JavaScript", "JavaScript", "Java", "Java", "Java", "JavaScript", "Java", "Ruby" };

        Map<String, Integer> decisionMap = new HashMap<>();
        for (String str : values) {
            if (decisionMap.containsKey(str)) {
                decisionMap.put(str, decisionMap.get(str) + 1);
                continue;
            }
            decisionMap.put(str, 0);
        }

        System.out.println(decisionMap.toString());

    }
}

Unique strings will have 0 against the key. You may now choose your preferred way to print them.

Arry
  • 107
  • 1
  • 4
0

Probably not most efficient, but an approach:

// input: String []  a
// initialize empty:
Set<String> u = new HashSet<>(a.length);
// initialize with all values of a:
List<String> d = new ArrayList<>(Arrays.asList(a));
for(String s:a) {
  if (u.add(a)) {// s is added to u for the first time:
    d.remove(a);
  } else { // s is duplicate in u:
    u.remove(a);
  }
}
String [] us = u.toArray(new String[0]);
String [] ds = d.toArray(new String[0]); 

...utilizing the characteristics of a set (element uniqueness) and Set.add():boolean method, which signals true, when "the set was modified by the add operation".

xerx593
  • 12,237
  • 5
  • 33
  • 64
0

for printing unique values you can go with the Collection framework

    HashSet<String> h = new HashSet<String>(); 
   //Adding elements into HashSet add() 
   for(int i = 0; i < length; i++)
   {
   h.add(a[i]);           //adding element in the set
   }
      // Iterating over hash set items 
   for (String i : h)
   {  
      System.out.println(i);     //printing unique elements  
    }
28rox
  • 11
  • 2
0

This is an another approach using java 8

public class Test {
    public static void main(String[] args) {
        String[] values = { "Java", "C", "JavaScript", "JavaScript", "Java", "Java", "Java", "JavaScript", "Java",
                "Ruby" };
        List<String> l = new LinkedList<>(Arrays.asList(values));// you can not use List<String> l = Arrays.asList(values); because we will be removing the duplicate values later so you will get java.lang.UnsupportedOperationException
        Set<String> allItems = new HashSet<String>();
        List<String> duplicates = Arrays.stream(values).filter(n -> !allItems.add(n)).collect(Collectors.toList());
        System.out.println(duplicates);//this will print all the duplicate values
        l.removeAll(duplicates);// input=duplicate+unique so removing the duplicate will give the unique values
        System.out.println(l);

    }

}

output:

[JavaScript, Java, Java, Java, JavaScript, Java]
[C, Ruby]
SpringLearner
  • 13,738
  • 20
  • 78
  • 116