# [chuck-users] Remove duplicates from array

David Powers cyborgk at gmail.com
Fri Apr 21 17:51:06 EDT 2006

Hello everyone,

I tried to send this earlier today, and it didn't seem to come through, but
in the meantime I did more work on the problem.

I am writing some functions that will be made into a class, for doing basic
serial operations on cells (i.e. arrays of floats). The operations I have so
far are: transpose, reverse, invert, permute, shuffle.

The next function to finish is multiply, which does Boulez-style cell
multiplication. For the function to work correctly, it should remove any
duplicates from the array. Unfortunately, my brain's a little rusty right
now. I'm sure I did this in the only computer class I ever took (C++). But
I'm too sleepy to figure out the correct solution offhand, I think the ways
I'm coming up with are inefficient. Can anyone help? My attempt at this is
in the function remDup below, but I made some mistake, I'm sure relating to
using a while loop--I'm not sure what the problem is though, or a better way
to do it.

I've gotten really spoiled, by usually coding these sorts of things in
Python, where its amazingly quick to do list manipulations of any kind.

//cellFun.ck

public class Argh
{
fun static void print(float cell[])
{
for (0 => int i; i<cell.cap(); i++)
{
<<< cell[i] >>> ;
}
}

fun static void remove (float a[], int index)
{
a.cap()-1 => int newLength;
float temp[newLength];
for (0 => int i; i < newLength; i++)
{
if (i<index)
{
a[i] => temp[i];
}
else
{
a[i+1] => temp[i];
}
}
float a[newLength];
for (0 => int i; i < newLength; i++ )
{
temp[i] => a[i];
}
}

//match
fun static int match(float cell[], float element)
{
cell.cap() => int length;
-1 => int position;
for (0 => int i; i<length; i++)
{
if (cell[i] == element)
{
i => position;
break;
}
}
return position;
}

fun static float[] copy(float cell[])
{
cell.cap() => int length;
float newCell[length];
for (0 => int i; i<length; i++)
{
cell[i] => newCell[i];
}
return (newCell);
}

//sort
fun static float[] sort(float cell[])
{
cell.cap() => int length;
float temp;
for (0 => int x ; x < length-1; x++)
{
for (0 => int y; y < length-x-1; y++)
{
if (cell[y] > cell[y+1])
{
cell[y] => temp;
cell[y+1] => cell[y];
temp => cell[y+1];
}
}
}
return cell;
}

fun static float[] remDup(float cell[])
{
1 => int i;
cell.cap() => int length;
while (i < length-1)
{
if (cell[i]==cell[i+1])
{
remove(cell,i+1);
}
else
{
i++;
}
}
return cell;
}

}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.cs.princeton.edu/pipermail/chuck-users/attachments/20060421/3083f5e8/attachment.htm