Posts Tagged ‘array’

Check size constraints for multidimensional arrays

Monday, February 8th, 2010

Multidimensional arrays are commonly used as a complex data structure. Moreover, they are used to convey data between software systems internally. In some programming languages however, there is no way to constrain the size of arrays at any stage of the array’s lifetime. PHP for instance has no ability to do this. Therefore a way must be devised to make sure the structure of the passed data conforms to what the receiving system expects.

Think for instance pixel convolution matrices. These are usually 3 by 3.

Check out my recursive PHP function to check sizes at any dimensional depth possible:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function isDimension($arr, $dimensions)
{
   $pass = true;
   $amount = array_shift($dimensions);
 
   if($amount === -1 || count($arr) === $amount)
   {
      if(!empty($dimensions))
      {
         foreach($arr as $element)
         {
            if(is_array($element))
            {
               if(!isDimension($element, $dimensions))
               {
                  $pass = false;
               }
            }
            else
            {
               $pass = false;
            }
         }
      }
   }
   else
   {
      $pass = false;
   }
   return $pass;
}

And when we utilize this function in the following way:

1
2
3
4
5
6
7
8
9
10
11
12
$subject = array(	array(1,2,3),
			array(2,3,4),
			array(3,4,5));
 
if(isDimension($subject, array(3,3)))
{
   print "Yay";
}
else
{
   print "Nay";
}

The output will be:

Yay

If at a certain dimension size does not matter you can skip it by entering -1 as the expected size:

1
2
3
4
5
6
7
8
$subject = array(	array(1,2,3),
			array(2,3,4),
			array(3,4,5)
			array(4,5,6));
 
$wellIsIt = isDimension($subject, array(-1,3)); // Returns true
$wellIsIt = isDimension($subject, array(4,3)); // Returns true
$wellIsIt = isDimension($subject, array(3,3)); // Returns false

This is a recursive function, meaning that it calls itself from within itself until a certain condition is met. Then it cascades the result back to the initial function call which in turn passes it back to whatever script called the function. More about this can be read on wikipedia.

There ya go

Javascript: Remove duplicates from Array

Friday, December 21st, 2007

Removing duplicate entries or values from a Javascript array is something which isn’t accomodated for by the native functions in Javascript. I searched Google for a few solutions but they were all lacking something in my opinion, be it performance or just sheer elegance.

*UPDATE*

There is now an array prototype function for removing duplicates. I suggest you use the prototype function instead of the functions below. It is the way it should have been done in the first place and is faster and moreover the most correct way to do it.

Array.prototype.unique = function () {
	var r = new Array();
	o:for(var i = 0, n = this.length; i < n; i++)
	{
		for(var x = 0, y = r.length; x < y; x++)
		{
			if(r[x]==this[i])
			{
				continue o;
			}
		}
		r[r.length] = this[i];
	}
	return r;
}

You can now utilize the unique function like this:

var arr = [1,2,2,3,3,4,5,6,2,3,7,8,5,9];
var unique = arr.unique();
alert(unique);

The result will be [1,2,3,4,5,6,7,8,9].

*UPDATE*

I therefore made two variants myself:

The first one does what you’ll expect, it’ll regard the first encountered entry as the original and all subsequent entries as duplicates.

function unique(a)
{
   var r = new Array();
   o:for(var i = 0, n = a.length; i < n; i++)
   {
      for(var x = 0, y = r.length; x < y; x++)
      {
         if(r[x]==a[i]) continue o;
      }
      r[r.length] = a[i];
   }
   return r;
}

If we pass the following array [1, 2, 3, 1, 4, 5] to the function the result will be [1, 2, 3, 4, 5].

The second variant returns different results. It will regard the last encountered duplicate as the original. This may be desirable in certain situations.

function unique(a)
{
   var r = new Array();
   o:for(var i = 0, n = a.length; i < n; i++) {
      for(var x = i + 1 ; x < n; x++)
      {
         if(a[x]==a[i]) continue o;
      }
      r[r.length] = a[i];
   }
   return r;
}

The output in this case will be [2, 3, 1, 4, 5].

I wrote both these functions with performance in mind. I have thoroughly tested and profiled both of them. Here are the benefits over other solutions to the problem:

  • it’s just one function
  • the loop length is determined through the object model just once per loop (see the for statements: var i = 0, n = a.length;)
  • after a duplicate has been detected in the nested loop it doesn’t iterate further but goes on to the next entry in the source array (see continue statement)

You can easily adapt the function to make it a prototype function of an Array object. If people don’t know how to do it just ask, i’ll add it.

Hope this helps someone out!