how to repeat number in array.

edited May 2016 in How To...

I don't know how to find repeat number simply, like find mode in a array.(using processing )

Answers

  • There might not be a simple way. You might have to program your own way.

    Imagine that you - not the computer - is trying to find repeated numbers in an array. What steps would you take to find them? What would you have to remember or keep track of while you're looking for them?

    What you need to remember are the variables you need to use.

    The steps you take become the code. Try writing it yourself!

  • edited May 2016

    https://Processing.org/reference/IntDict.html

    // forum.Processing.org/two/discussion/16731/how-to-repeat-number-in-array
    // GoToLoop (2016-May-21)
    
    final int QTY = 10, RANGE = 10;
    final byte[] vals = new byte[QTY];
    
    for (int i = 0; i < QTY; vals[i++] = (byte) random(RANGE));
    println(vals);
    println();
    
    IntDict count = new IntDict();
    for (final byte num : vals)  count.increment(str(num));
    count.sortValuesReverse();
    println(count);
    
    for (final String k : count.keyArray())  if (count.get(k) < 2)  count.remove(k);
    println(count, ENTER);
    
    count.sortKeys();
    byte[] repeats = byte( int(count.keyArray()) );
    println(repeats);
    
    exit();
    
Sign In or Register to comment.