Click to See Complete Forum and Search --> : Easy question....


mawood
01-31-2003, 11:56 AM
...or maybe not.

How would I go about creating an array of values and then checking them to look for duplicates?

I've made a cookie generated navigation (my favorites), but the users are able to create re-peat links. I need to create a loop that will find repeat links, the rest I can do.

Zach Elfers
01-31-2003, 12:04 PM
Try something with a loop within a loop like this:

array1 = new Array("1","2","3");
array2 = new Array("4","5","2");

for (a = 0; a < array1.length; a++) {
for (b = 0; b < array2.length; b++) {
if (a == b) {
alert("There are duplicates!");
}
}
}

Hope that helps! :)

mawood
01-31-2003, 12:12 PM
This much I understand. The problem is this: The number of links found in the cookie varies. I need to set an array based on the number of links found, then go through them to check for duplicates. I am on a similar track as the code above, but you have defined 3 values in the above array, I need to set a varying number of them.

Thank you in advance.

mawood
01-31-2003, 12:27 PM
I could work with the following:

<SCRIPT LANGUAGE="JavaScript">

array1 = new Array("1","2","3");
array2 = new Array("4","5","1");

for (a = 0; a < array1.length; a++) {
for (b = 0; b < array2.length; b++) {
if (array1[b] == array2[a]) {
alert("There are duplicates! - " + array2[a] + " & " + array1[b]);
}
}
}

</SCRIPT>


...but the first two lines. Just need to make that run through a loop that I already have to go through the cookie and set the array.

Zach Elfers
01-31-2003, 12:36 PM
Just change the arrays to the arrays you have.

mawood
01-31-2003, 01:35 PM
Yes, but if I create the arrays, they automatically duplicate. If the user has three links: Hardware, Software and Requests, array 1 will have values of: Hardware, Software and Requests. Also array 2 will values of Hardware, Software and Requests. This will make for three duplicates when in fact there are none.

mawood
01-31-2003, 01:39 PM
Ok, I think I may have it with this. Thanks for your help!!!




var checkDup = new Array;
var checkDupp = new Array;

for (i=1; i <= NumToDoItems; i++) {
ToDoItem = GetCookie('PT_ToDoItem'+i);
if (ToDoItem != null) {
checkDup[i] = ToDoItem;
}
}

for (i=1; i <= NumToDoItems; i++) {
ToDoItem = GetCookie('PT_ToDoItem'+i);
if (ToDoItem != null) {
checkDupp[i] = ToDoItem;
}
}

for (a = 0; a < checkDup.length; a++) {
for (b = 0; b < checkDupp.length; b++) {
if (a != b) {
if (checkDup[b] == checkDupp[a]) {
alert("There are duplicates! - " + checkDupp[a] + " & " + checkDup[b]);
}
}
}
}

Zach Elfers
01-31-2003, 01:43 PM
Instead of using:

var checkDup = new Array;

use:

var checkDup = new Array();

mawood
01-31-2003, 02:12 PM
THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU!! Works well:





var deleteDups = 0;




var checkDup = new Array();
var checkDupp = new Array();

for (i=1; i <= NumToDoItems; i++) {
ToDoItem = GetCookie('PT_ToDoItem'+i);
if (ToDoItem != null) {
checkDup[i] = ToDoItem;
}
}

for (i=1; i <= NumToDoItems; i++) {
ToDoItem = GetCookie('PT_ToDoItem'+i);
if (ToDoItem != null) {
checkDupp[i] = ToDoItem;
}
}

for (a = 0; a < checkDup.length; a++) {
for (b = 0; b < checkDupp.length; b++) {
if ((a != b) && (checkDup[a] != null) && (checkDupp[b] != null)) {

if (checkDup[b] == checkDupp[a]) {
deleteDups++;
if (deleteDups == 2) {
alert("You have selected a Favorite that already exists. Deleting last choice.");
DeleteItem(a);
break;
}
}
}
}
}