3 Replies Latest reply on Oct 17, 2006 8:47 AM by Newsgroup_User

    Checking dublicate valuess in Arrays

    gregmax17
      Just like the title states, 'How do you check if an array has dublicate values?' The reason for me asking is because I am developing a game where the player/user colletects items, and is stored in an array. But how do I check to see if the player has 3 blue potions, or 2 red potions , etc

      this is what I have:

      var item = new Array()
      item[0] = "Red Potion";
      item[1] = "Bread";
      item[2] = "Blue Potion";
      item[3] = "Red Potion";
      etc

      Thanks
        • 1. Re: Checking dublicate valuess in Arrays
          kglad Adobe Community Professional & MVP
          you could loop through your array and count all the various duplicates but it would probably be more efficient to use a 2d array that would contain distinct items and the number of times it was added.

          you could create a function that would push [itemName,1] if itemName is not in your 2d array (say item2D) and increment item2D[indexNum][1] when itemName is already in your array and has index = indexNum.
          • 2. Re: Checking dublicate valuess in Arrays
            Level 7

            "kglad" <webforumsuser@macromedia.com> wrote in message
            news:eh2qpq$fqu$1@forums.macromedia.com...
            > you could loop through your array and count all the various duplicates but
            > it
            > would probably be more efficient to use a 2d array that would contain
            > distinct
            > items and the number of times it was added.
            >
            > you could create a function that would push [itemName,1] if itemName is
            > not in
            > your 2d array (say item2D) and increment item2D[indexNum][1] when itemName
            > is
            > already in your array and has index = indexNum.
            >

            Going on what kglad states above you could also use an Object with fixed
            values for the properties...

            So, what I mean is, if you know what all of the possible elements of the
            inventory are going to be ahead of time then you can just create your Object
            ahead of time and not worry about checking for an items existence, instead
            you could just have value for each property be the number of items in
            inventory.

            Example:

            inventory_items = {'Blue Potion':0, 'Red Potion':0, 'Bread':0 .....};

            then, when they aquire a new 'Blue Potion' is is just:
            inventory_items["Blue Potion"] += 1;

            and when they use one
            inventory_items["Blue Potion"] -= 1;

            of course it would be much better to throw this in a function and then you
            could do other things like stop an inventory items quantity from going below
            0 and above some upper limit (based on total number of items maybe, since a
            person can only carry so much.

            HTH


            • 3. Re: Checking dublicate valuess in Arrays
              Level 7
              You can't use 'Blue Potion', 'Red Potion', etc as identifiers. The compiler
              will not allow it.

              > inventory_items = {'Blue Potion':0, 'Red Potion':0, 'Bread':0 .....};