-1

I'm having this

String array[] = {"test","testing again", "test"};

that i want to mark and remove duplicates. Here is the output that i need:

2x test

testing again

Can someone help me do this? I've tried with Set but it seems it doesnt recogize when a string is already inthere.

Here is my code:

Set addons = new HashSet<String>();
final String[] arr ={"test","testing again", "test"};
            for (int i = 0; i < arr.length; i++) {
                Log.d(TAG, "contains adding " + arr[i]);

                if (addons.contains(arr[i])) {
                    //never enters here
                    Log.d(TAG, "contains " + arr[i]);
                    addons.remove(arr[i]);
                    addons.add("2 x " + arr[i]);
                } else {
                    addons.add("1 x " + arr[i]);
                }
            }
Darko Petkovski
  • 3,892
  • 13
  • 53
  • 117

10 Answers10

2

You can do something like this:

String[] arr = { "test", "testing again", "test" };
HashMap<String, Integer> counter = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
    if (counter.containsKey(arr[i])) {
        counter.put(arr[i], counter.get(arr[i]) + 1);
    } else {
        counter.put(arr[i], 1);
    }
}
System.out.println("Occurrences:\n");
for (String key : counter.keySet()) {
    System.out.println(key + " x" + counter.get(key));
}

Your example doesn't work because, when you find a new occurrence of a word you remove it and replace it with something like 2x [word], when that word comes up again contains(...) will return false since it's no longer in the set.

Titus
  • 22,031
  • 1
  • 23
  • 33
2

In java 8:

Stream.of("test", "testing again", "test")
        .collect(groupingBy(Function.identity(), counting()))
        .forEach((str, freq) -> {
            System.out.printf("%20s: %d%n", str, freq);
        });
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
1

try this:

 public static void main(String[] args) {

        Set<String> addons = new HashSet<>();
        final String[] arr = { "test", "testing again", "test","test","testing again" };
        int count = 0;
        for (int i = 0; i < arr.length; i++) {

            for (int j = 0; j < arr.length; j++) {
                if (arr[i].equals(arr[j])) {
                    count++;
                }
            }

            addons.add(count + " x " + arr[i]);
            count = 0;
        }

        System.out.println(addons);

    }

output:

[2 x testing again, 3 x test]
Rustam
  • 6,485
  • 1
  • 25
  • 25
0
String[] arr ={"test","testing again", "test"};
Map<String, Integer> results = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
  Log.d(TAG, "contains adding " + arr[i]);
  if (results.containsKey(arr[i])) {
      Log.d(TAG, "contains " + arr[i]);
      results.put(arr[i], results.get(arr[i]) + 1);
  } else {
      results.put(arr[i], 1);
  }
}  
agad
  • 2,192
  • 1
  • 20
  • 32
0

try following code.

String[] array ={"test","testing again","test"};
Set<String> uniqueWords = new HashSet<String>(Arrays.asList(array));
Ganpat Kaliya
  • 888
  • 2
  • 9
  • 16
0

The problem is that you don't directly add "test" in your Set, but "1 x test" instead.

So you better use a Map to save the string, and its number of occurence.

    String[] array = { "test", "testing again", "test" };
    Map<String, Integer> addons = new HashMap<>();

    for (String s : array) {
        System.out.println("Dealing with [" + s + "]");
        if (addons.containsKey(s)) {
            System.out.println("\tAlready contains [" + s  + "]");
            addons.put(s, addons.get(s) + 1); // increment count of s
        } else {
            System.out.println("\tFirst time seeing [" + s  + "]");
            addons.put(s, 1); // first time we encounter s
        }
    }
Baptiste
  • 449
  • 4
  • 7
0

Use this, where Key to the Map is the String element and Value is the Count of that element.

public static void main(String[] args) {
        String array[] = {"test","testing again", "test"};

        Map<String, Integer> myMap = new HashMap<>();

        for (int i = 0; i < array.length; i++) {
            if (myMap.containsKey(array[i])) {
                Integer count = myMap.get(array[i]);
                myMap.put(array[i], ++count);
            } else{
                myMap.put(array[i], 1);
            }
        }

        System.out.println(myMap);
    }
Ankush soni
  • 1,439
  • 1
  • 15
  • 30
0
String array[] = {"test","testing again", "test"};
Map<String, Integer> countMap = new HashMap<>();
for (int i = 0; i<array.length; i++) {
    Integer count = countMap.get(array[i]);
    if(count == null) {
        count = 0;
    }
    countMap.put(array[i], (count.intValue()+1));
}
System.out.println(countMap.toString());

Output

{'test'=2, 'testing again'=1}
Shahzeb
  • 3,696
  • 4
  • 28
  • 47
0

You can use Multiset from Guava.

String array[] = {"test","testing again", "test"};
Multiset<String> set = HashMultiset.create(Arrays.asList(array));
System.out.println(set);

Output:

[test x 2, testing again]

Multiset basically counts how many times you tried to add an object.

for (HashMultiset.Entry<String> entry :set.entrySet()) {
    System.out.println(entry.getCount() + "x " + entry.getElement());
}

Output:

2x test 
1x testing again
g-t
  • 1,455
  • 12
  • 17
0

You can use own class that hold duplicates:

class SetWithDuplicates extends HashSet<String> {

   private final Set<String> duplicates = new HashSet<>();

    @Override
    public boolean add(String e) {
       boolean added = super.add(e);
       if(!added) {
           duplicates.add(e);
       }
       return added;
    }

    public Set<String> duplicates() {
        return duplicates;
    }

}

And use that similarly like @Ganpat Kaliya:

String[] array ={"test","testing again","test"};
SetWithDuplicates <String> uniqueWords = new SetWithDuplicates(Arrays.asList(array));
SetWithDuplicates <String> duplicates = uniqueWords.duplicates();
hexin
  • 947
  • 7
  • 17