# Count identical strings in array for each unique entry

**URL:** <https://discourse.processing.org/t/count-identical-strings-in-array-for-each-unique-entry/16678>\
**Category:** Coding Questions\
**Created:** [December 28, 2019, 4:33pm UTC](https://discourse.processing.org/t/count-identical-strings-in-array-for-each-unique-entry/16678 "2019-12-28T16:33:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndreasRef](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/andreasref/32/133_2.png) [@AndreasRef](https://discourse.processing.org/u/AndreasRef)\
**Post date:** [December 28, 2019, 4:33pm UTC](https://discourse.processing.org/t/count-identical-strings-in-array-for-each-unique-entry/16678/1 "2019-12-28T16:33:53Z")

</div>

If I have an array of strings, how do I count the number of times each unique string occurs in the array? I do not know the content of the array beforehand.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 28, 2019, 4:36pm UTC](https://discourse.processing.org/t/count-identical-strings-in-array-for-each-unique-entry/16678/2 "2019-12-28T16:36:03Z")

</div>

Look at HashMap please.

Store the words in the HashMap.

- When a entry is there already, increment its value by 1,
- otherwise make a new entry

[https://www.processing.org/reference/HashMap.html](https://www.processing.org/reference/HashMap.html)

Chrisir

---

<div class="post-metadata">

**Author:** ![AndreasRef](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/andreasref/32/133_2.png) [@AndreasRef](https://discourse.processing.org/u/AndreasRef)\
**Post date:** [December 28, 2019, 5:42pm UTC](https://discourse.processing.org/t/count-identical-strings-in-array-for-each-unique-entry/16678/3 "2019-12-28T17:42:47Z")

</div>

Awesome, would you do it like this?

```auto
import java.util.Map;
HashMap<String,Integer> hm = new HashMap<String,Integer>();
String[] myStrings = {"dog", "dog", "cat", "dog"};

for (int i = 0; i<myStrings.length; i++) {
  int count = hm.containsKey(myStrings[i]) ? hm.get(myStrings[i]) : 0;
  hm.put(myStrings[i],count+1);  
}
println(hm);

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 28, 2019, 5:44pm UTC](https://discourse.processing.org/t/count-identical-strings-in-array-for-each-unique-entry/16678/4 "2019-12-28T17:44:35Z")

</div>

There are many ways to do it. Here’s an interesting 1 using [StringList](https://processing.org/reference/StringList.html) + [IntDict](https://processing.org/reference/IntDict.html):

- [Processing.GitHub.io/processing-javadocs/core/processing/data/StringList.html#getTally--](http://Processing.GitHub.io/processing-javadocs/core/processing/data/StringList.html#getTally--)
- [Processing.GitHub.io/processing-javadocs/core/processing/data/StringList.html#getUnique--](http://Processing.GitHub.io/processing-javadocs/core/processing/data/StringList.html#getUnique--)

```auto
// https://Discourse.Processing.org/t/
// count-identical-strings-in-array-for-each-unique-entry/16678/4

// GoToLoop (2019/Dec/28)

final String[] animalsArray = {
  "cat", "dog", "ferret", "ferret", "cat", "parrot", "dog", "macaw", "cat"
};
println(animalsArray);

final StringList animalsList = new StringList(animalsArray);
println(animalsList);

final IntDict animalsTally = animalsList.getTally();
println(animalsTally);

final String[] animalsUnique = animalsList.getUnique();
println(animalsUnique);

exit();

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 28, 2019, 6:36pm UTC](https://discourse.processing.org/t/count-identical-strings-in-array-for-each-unique-entry/16678/5 "2019-12-28T18:36:11Z")

</div>

@AndreasRef

looks great!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 28, 2019, 6:55pm UTC](https://discourse.processing.org/t/count-identical-strings-in-array-for-each-unique-entry/16678/6 "2019-12-28T18:55:38Z")

</div>

same sketch

```auto

import java.util.Map;

HashMap<String, Integer> hm = new HashMap<String, Integer>();

String[] myStrings = {
  "dog", "dog", "cat", "dog", "bird"
};

void setup() {
  size(600, 600); 
  background(0); 

  addWordsToHashMap(myStrings);
}

void draw() {
  background(0); 

  showHashMap();
}

// ---------------------------------------------------------------------

void addWordsToHashMap(String[] myStrings) {
  for (int i = 0; i<myStrings.length; i++) {
    // if it's old
    if (hm.containsKey(myStrings[i])) {
      // increase
      increaseHashMapByOne(myStrings[i]);
    } else { 
      // add
      addToHashMap(myStrings[i]);
    }//else
  }//for
  //
}//func 

void increaseHashMapByOne(String search) {
  // requires that search exists 
  int count = hm.get(search); 
  hm.put(search, count+1); // increase
}

void addToHashMap(String newWord) {
  hm.put(newWord, 1); // add to HashMap
}

void showHashMap() {
  // show with text() 
  // Using an enhanced loop to iterate over each entry
  int i=0; 
  for (Map.Entry me : hm.entrySet()) {
    text(me.getKey() + " is ", 33, 30+i*17);
    text(str((int)me.getValue()), 103, 30+i*17);
    i++;
  }
}
//

```

---

<div class="post-metadata">

**Author:** ![AndreasRef](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/andreasref/32/133_2.png) [@AndreasRef](https://discourse.processing.org/u/AndreasRef)\
**Post date:** [January 9, 2020, 12:24pm UTC](https://discourse.processing.org/t/count-identical-strings-in-array-for-each-unique-entry/16678/7 "2020-01-09T12:24:41Z")

</div>

Here is the project I ended up making. Automatically generated fairy tales from detected objects in mundane images. Built with YOLOv3, XLNet, Processing And Runway

 ![cake_dog](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/5139a564f0ff5cfae14764e0062f071f7fe4e58e.jpeg) ![clock](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/2fafdb00e8bca82e9de7971f06f119f2f5a19117.jpeg) ![horse_dog](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b54b5ae1b3ed187834732caee8b485aa04af2ea7.jpeg) ![keyboard_tvmonitor_person](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/3e5e99758df9a5ce799496349bbb87ee60649676.jpeg) ![suitcase](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/ccb1cc51efff5820da94b3e2eec757a5f2e52258.jpeg) ![toilet](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a007a54a5fc9b2534e3a3ba61e2a7b928b53c525.jpeg)

More info here:  
[https://andreasrefsgaard.dk/project/fairy-tales/](https://andreasrefsgaard.dk/project/fairy-tales/)
