0

does anyone have any idea what to do in order to find the number of sequences in an array?

for example, my array is:

var numbers:Array = new Array(banana, banana, apple, banana, banana);

and i need to find is: * how many times there is a sequence of "banana" * and the length of each sequence.

what shell i do in order to get the following result: 2,1,2 (2 bananas, 1 apple, 2 bananas)

i tried with do while loop, but i i guess i miss something.

a short example will be very appreciated!

thanx

3
  • 1
    what objects are banana and apple? they clearly are not strings. Commented Feb 28, 2012 at 16:20
  • Please share code for what you tried. Also as an FYI; you can format code using the Curly bracket button in the question editor. It'll make your question, especially code, easier to read. Commented Feb 28, 2012 at 16:20
  • hey, you right. my question wasn't clear enough. here are some explanations: banana and apple are strings. what i wish to find is the sequences of the bananas only: for example - for this array: var numbers:Array = new Array(banana, banana, apple, banana, banana); i would like to get "2,2" . thanx again for any help! Commented Feb 28, 2012 at 17:29

5 Answers 5

1
var prev:String = null;
var q:int = 0;
var result:Array = new Array();
for(var i:int=0; i<numbers.length; ++i){
  if(prev!=numbers[i]){
    if(q>0) result.push(q);
    q=1;
    prev=numbers[i];
  }
  else ++q;
}
if(q>0) result.push(q);

This is, assuming banana, etc. are strings (probably a typo above?). It would be simple to modify to other types of objects

Sign up to request clarification or add additional context in comments.

2 Comments

hey, thanx a lot! i was not clear enough in my question. and i wonder - in case i'd like to return only the sequences of the bananas - what should i do? thanx a lot again.
if((q>0)&&(prev=="bananas")) should do.
0

Really all you want to know is whether the string at index n equals the string at index n+1...

var targetIndex:int = numbers.length - 1;
var results:Array = [1];
var resultsIndex:int = 0;
for(var n:int = 0; n < targetIndex; n++) {
    if(numbers[n] == numbers[n+1]) {
        results[resultsIndex]++;
    } else {
        results[++resultsIndex] = 1;
    }
}
trace(results.join(','));

Comments

0
function sequencesInArray(array:Array):Array {
    var sequence:Array = [];
    var currSequenceCount:uint = 1;
    for (var i:uint = 1; i < numbers.length; i++) {
        if (numbers[i - 1] != numbers[i]) {
            sequence.push(currSequenceCount);
            currSequenceCount = 1;
        } else {
            currSequenceCount++;
        }
    }
    return sequence;
}

Then:

var banana:int = 1;
var apple:int = 2;
sequencesInArray([banana, banana, apple, banana, banana]); //returns: [2, 1, 2]

Comments

0

In the question you don't define banana and apple, anyway I would use a map or a Dictionary to store key/value pairs, with the key being the string/object you want to count and the value being the counter of the object occurences in your array.

var objectsCounter:Dictionary = new Dictionary();

for (var key:String in numbers) 
{
    if ( objectsCounter[key] )
        objectsCounter[key] = objectsCounter[key] + 1;
    else 
        objectsCounter[key] = 1;
}

This way you can store any type in the dictionary.

edit:

for (var key:String in objectsCounter) 
{
    // iterates through each object key
}

for each (var value:Number in objectsCounter) 
{
    // iterates through each value
}

Comments

0

I believe this is what you're looking for:

    var array:Array = [
        "banana", "banana",
        "apple",
        "banana", "banana", "banana"
    ];

    var sequences:Array = findSequences(array, "banana");

    trace("sequences:", sequences); // prints "sequences: 2,3"

And:

private function findSequences(array:Array, searchElement:*):Array
{
    var sequences:Array = [];
    var currentSequence:int = 0;

    for each (var element:* in array) {
        if (element == searchElement) {
            currentSequence++;

        } else if (currentSequence > 0) {
            sequences.push(currentSequence);
            currentSequence = 0;
        }
    }

    if (currentSequence > 0) {
        sequences.push(currentSequence);
    }

    return sequences;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.