How to find the frequency of occurrence of a number or a string in an array

edited October 2015 in Programming Questions

Hello everyone,

    int [] number= {4,5,6,3,5,3,5,6,6,8};
    String [] animal= {cat,dog,rat,cat,cat,dog,dog,dog,cat,rat};
    void setup(){
    size(800,800);
    smooth();

    }

    void draw(){
    background(0);

    }

I would like to know how can I write a code that will find me the repeated occurrence of each of the number/string in the array.

my aim is to list the numbers and strings as follows

Number 3 = 2 Number 4 = 1 Number 5 = 3 Number 6 = 3 Number 8 = 1

cat = 4 Dog = 4 rat =2

Any help will be greatly appreciated.

Many thanks in advance :)

Answers

  • I hand you a bag of marbles and ask you how many of each color are in the bag.

    How do you, as a person, solve this problem?

    Can you describe the process you use?

    Using that description, can you explain the process in a way that my pet robot can understand?

    Try your hand at this first - post your results. Do not jump right into code. English can also be used to describe directions.

  • make a variable called numDog,numRat,numCat have a for loop that runs through that entire array and searches for an element and then increase that variable by 1. You can then print it in the draw() method

  • Dear TfGuy44, Thank you for your reply, :) and sorry for my less described, informed question.

    So i tried breaking down the task in to the following steps

    I have an array of numbers for example: 1,2,3,3,4,3,4,5,2 I wanted to group the repeating same numbers together and count how many time each of the numbers repeat in the given array. then making a table with two columns, showing the numbers in the array on one side and their repeating occurrence on the other side

    I hope this description makes sense :)

  • edited June 2015

    Dear Lonlyancats,

    Thank you so much for your help! :) shall try doing it, Although I am a beginner in coding

  • edited June 2015 Answer ✓

    A possible solution is simply create some HashMap to store the pair {key : repeats}:
    https://processing.org/reference/HashMap.html

    For the int[] array, the pair is Map<Integer, Integer>:
    final Map<Integer, Integer> intAmount = new HashMap<Integer, Integer>();

    And for the String[] array, it is now Map<String, Integer>:
    final Map<String, Integer> petAmount = new HashMap<String, Integer>();

    However for Map<String, Integer>, Processing got a more simplified container called IntDict:
    https://processing.org/reference/IntDict.html

    So let's use it for this specific Map<String, Integer> case instead:
    final IntDict petAmount = new IntDict();

    For the Map container, we're gonna need its get() & put() methods:
    http://docs.oracle.com/javase/8/docs/api/java/util/Map.html#get-java.lang.Object-
    http://docs.oracle.com/javase/8/docs/api/java/util/Map.html#put-K-V-

    And the correspong 1s for IntDict are get() & set():
    https://processing.org/reference/IntDict_get_.html
    https://processing.org/reference/IntDict_set_.html

    And finally, here's the full solution. Any further doubts just ask here again: O:-)

    P.S.: Newer version replacing IntDict's get() & set() methods w/ increment(): :P
    https://processing.org/reference/IntDict_increment_.html

    // forum.processing.org/two/discussion/11275/
    // how-to-find-the-frequency-of-occurrence-of-a-number-or-a-string-in-an-array
    
    // 2015-Jun-11
    
    import java.util.Map;
    
    final Map<Integer, Integer> intAmount = new HashMap<Integer, Integer>();
    final IntDict petAmount = new IntDict();
    
    static final int[] INTS = {
      4, 5, 6, 3, 5, 3, 5, 6, 6, 8
    };
    
    static final String[] PETS = {
      "cat", "dog", "rat", "cat", "cat", "dog", "dog", "dog", "cat", "rat"
    };
    
    void setup() {
      for (Integer i : INTS) {
        Integer qty = intAmount.get(i);
        intAmount.put(i, qty == null? 1 : qty + 1);
      }
    
      // for (String s : PETS) {
      //   int qty = petAmount.get(s, 0);
      //   petAmount.set(s, qty + 1);
      // }
    
      for (String s : PETS)  petAmount.increment(s);
    
      println(intAmount);
      println(petAmount);
    
      exit();
    }
    

    P.S.: For better code format in this forum, read this article please:
    http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text

  • Thank you so much GoToLoop!!!

    Now I will sit and learn this :)

Sign In or Register to comment.