Click to See Complete Forum and Search --> : checking for uniqueness of values in array
jimhsu
02-18-2003, 10:00 PM
How do you check if the members in a specific array are all unique?
For example, you have array n, composed of 100 entries.
I want to find a way to check if all the members of the array are different (unique), or say, members 0-14, 21-25, etc....
Is this possible? BTW if it helps, I'm working with integers.
AdamBrill
02-18-2003, 10:15 PM
Try this:
found=false;
for(x=0;x<arrayname.length;x++)
{
for(y=0;y<arrayname.length;y++)
{
if(arrayname[x]==arrayname[y] && x!=y)
{
found=true;
break;
}
}
if(found==true)
{
break;
}
}
if(found==true)
{
//this will run only if two are the same
alert("Two were the same.");
}
Change all of the arrayname's to whatever your array name is and it should work. It will run the alert("Two were the same."); only if there were two that were the same. Let me know if you have any problems...
Dan Drillich
02-18-2003, 10:32 PM
You can simplify the above solution by starting with sorting the array numerically.
<script>
var a = new Array();
a[0] = 11;
a[1] = 3;
a[2] = 012;
a.sort(function(a,b) {return a-b;});
alert(a[0]);
</script>
AdamBrill
02-18-2003, 10:42 PM
Dan - What exactly would that help with? I guess I'm not sure why sorting it makes it simpler. Please explain...
jimhsu
02-18-2003, 11:00 PM
Thanks all of you for your replies.
I am trying to solve a bonus problem for out math class. :)
The problem is:
A chemist has a 40lb wright and accidentally drops it on the floor. It shatters into 4 pieces, each being a integer unit of weight (ex. 3lb, 5lb.) He finds out that he can weigh anything from 1-40lb with the broken pieces.
Example:
a weight shatters into 2 pieces, one 8lb, other 3lb
You can measure 11lb, since 8+3 = 11, and you can put them on a lever.
You can measure 8lb and 3lb, since just put the weights on a lever.
You can also measure 5lb, since you can put the object with the 3lb weight, to make 8-3 = 5 or 3+5=8.
You can use any number of weights combined to weigh (max being 4)
Just a problem to think about... ;)
Dan Drillich
02-18-2003, 11:19 PM
AdamBrill,
I just think the following is lovely :cool:
<script>
var a = new Array();
a[0] = 11;
a[1] = 3;
a[2] = 012;
a[3] = 0121;
a.sort(function(a,b) {return a-b;});
var found = false;
var i = 1;
while (! found && i < a.length) {
if (a[i] == a[i-1]) {
found = true;
}
i++;
}
alert(found);
</script>
AdamBrill
02-18-2003, 11:28 PM
Dan - Ok, now I get it. The only thing is that the array would end up in numberical order, which he may or may not want. So, the best way would be Dave's method, since it doesn't put it in order and it is easier than mine. ;) In your method, however, you could just copy the array and check through the copy; then it would leave the original array in the correct order.
jimhsu - I figured out your math problem. It took me about 5 minutes to solve it. But, since it is for math class, I can't tell you the answer, so, you'll have to figure it out on your own... ;)