/    Sign up×
Community /Pin to ProfileBookmark

Easy question….

…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.

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@Zach_ElfersJan 31.2003 — 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! ?
Copy linkTweet thisAlerts:
@mawoodauthorJan 31.2003 — 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.
Copy linkTweet thisAlerts:
@mawoodauthorJan 31.2003 — 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.
Copy linkTweet thisAlerts:
@Zach_ElfersJan 31.2003 — Just change the arrays to the arrays you have.
Copy linkTweet thisAlerts:
@mawoodauthorJan 31.2003 — 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.
Copy linkTweet thisAlerts:
@mawoodauthorJan 31.2003 — 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]);

}

}

}

}
Copy linkTweet thisAlerts:
@Zach_ElfersJan 31.2003 — Instead of using:

var checkDup = new Array;

use:

var checkDup = new Array();
Copy linkTweet thisAlerts:
@mawoodauthorJan 31.2003 — 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;

}

}

}

}

}
×

Success!

Help @mawood spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...