ActionScript 3.0 – How to find duplicate array elements ? [SOLVED]



    private function hasDuplicates(arr:Array):Boolean{

      var x:uint;
      var y:uint;

    			for (x = 0; x < arr.length ; x++){

        			for (y = x + 1; y < arr.length; y++){

            				if (arr[x] === arr[y]){
                				return true;
            				}

        			}
    			}
    			return false;
		}

  

This small function returns true if array has duplicate elements  or false if there are no duplicates.

So if you have array “test1” and you need to make sure there are no duplicates, you could do something like this:


  if( !hasDuplicates(test1) ) // if there are no duplicates it will return "true" in this case

           trace ("There are no duplicates in array test1");

    

Have fun,

Anatoly

Advertisements

About Anatoly Spektor

My name is Anatoly Spektor (originally Anatolijs Spektors) I am Software and Web Developer. I have worked in Seneca Center for Development of Open Technology on Big Blue Button Add-on - Polling Module, Red Hat and some other places :) I am an author of the book 'Eclipse Debugging How To', Muay Thai fighter and amateur photographer ;)
This entry was posted in ActionScript3, Flex4, open-source, Programming Languages, Project BBB, Useful Functions and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s