We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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 hope this description makes sense :)
Dear Lonlyancats,
Thank you so much for your help! :) shall try doing it, Although I am a beginner in coding
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
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 :)