/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Searching array for duplicates … better ways?

The following script seems to work, but looks slow to me.
The purpose is to look at an array (recs) of strings with fields separated by ‘|’ characters.
By looking at certain fields (1 and 2) to form a key in uppercase,
I want to see if that is a duplicate key in the new ‘Order’ array.
I either save the key and position ‘p’ in a new element of ‘Order’
or I update it as a pointer to the most recent entry ‘recs’ if a duplicate is found.
Finally I sort the new ‘Order’ array and later parse the pointers (p) out so I can display the most recent ‘recs’ entry.

[code=php]
var Order = new Array(); // create artificial hash of KEYS|pointer
var str, i, j, p, fnd;
for (i=0; i<recs.length; i++) { // go thru database one time
p = i; // save DBase position
tmp = recs[i].split(‘|’); // get fields of each DBase record
// check for duplicate or revised entry.
str = (tmp[1]+tmp[2]).toUpperCase(); // form KEY of certain fields
for (j=0; j<Order.length; j++) { // look at prior elements of ‘Order’ array
fnd = false;
if (Order[j].match(str) != null) { p = j; Order[j] = str+’|’+p; fnd = true; } // if match found, update pointer
}
if (fnd == false) { Order.push(str+’|’+p); } // if no match, then create “KEY|pointer”
}
Order.sort();
[/code]

Is there a more efficient way to check for duplicate entries and update to the most recent entry in recs if found
without using the ‘match()’ or without going through the ‘Order’ array repeated times with increasing elements?

The code works … I’m just looking for a more efficient method as the ‘Order’ array becomes larger and larger.

Any ideas are appreciated as I’m willing to try other approaches if necessary.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@mrhooMar 01.2008 — why not make Order an object? each new Order[key] (Order[tmp[1]+tmp[2]]) assignment will replace any with the same key.
Copy linkTweet thisAlerts:
@JMRKERauthorMar 01.2008 — 'mrhoo':

I'll give that a try, assuming:
[code=php]
var Order = new object;
[/code]

is the correct way to create the object?

I'll post back if I have problems. Thanks.
Copy linkTweet thisAlerts:
@JMRKERauthorMar 01.2008 — Well, I didn't have problems after I changed to this,

but I also didn't get anything to display in the alert.

I must be doing something wrong with this:[code=php]
// var Order = new object; // got errors with this line instead of next
var Order = new Array();

for (i=0; i<recs.length; i++) {
tmp = recs[i].split('|');
str = (tmp[1]+tmp[2]).toUpperCase();
Order[str] = str+'|'+i;
// Order[str] = i; // would like to use this, but don't know how to sort keys here.
}
Order.sort();
alert(Order.join('n'));
[/code]
Copy linkTweet thisAlerts:
@JMRKERauthorMar 01.2008 — With more research and trial, got what I want. Now it screams! ?

For others who are interested:
[code=php]
var Order = new Object();

for (i=0; i<recs.length; i++) {
tmp = recs[i].split('|');
str = (tmp[1]+tmp[2]).toUpperCase();
Order[str] = i; // overwrites contents of element if changed
}
SortedOrder = new Array();
for (x in Order) { SortedOrder[SortedOrder.length] = x+'|'+Order[x]; }
SortedOrder.sort();
Order = []; // remove assoc array as is not needed anymore
[/code]

Then I display the records (recs) by decoding the pointers in the SortedOrder array

Thanks for the point into the right direction 'mrhoo'
×

Success!

Help @JMRKER 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 4.29,
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,
)...