sort out duplicated fax numbers

T

Tony WONG

i have a number of forms with fax numbers to come up into arrays and then
combine to string.

after that i design the flow
1. break the string to array
now the string looks like this 12345678,23456789,34567890...
2. check record-2 again record-1,
check record-3 again record-2 & record-1
check record-4 again record-3 & record-2 & record-1
and so on... (if duplicated, drop it)

problem - step 1 break the string to array
var CDEF = new Array(faxnumbers);
alert(CDEF[0],

it returns all the numbers (12345678,23456789,34567890...

i just wish to have 12345678

anything wrong with my script?

Grateful if you could kindly advise whether step 2 is the best way to sort
out duplicated numbers?

Thanks a lot

tony
 
R

RobG

Tony said:
i have a number of forms with fax numbers to come up into arrays and then
combine to string.

after that i design the flow
1. break the string to array
now the string looks like this 12345678,23456789,34567890...
2. check record-2 again record-1,
check record-3 again record-2 & record-1
check record-4 again record-3 & record-2 & record-1
and so on... (if duplicated, drop it)

problem - step 1 break the string to array
var CDEF = new Array(faxnumbers);
alert(CDEF[0],

it returns all the numbers (12345678,23456789,34567890...

i just wish to have 12345678

anything wrong with my script?

Yes, it's very inefficient. If you start with a comma-delimited set of
numbers, you can split it into an array using split:

var faxNumbers = '1234,2345,3456,45646,...';
var faxNumArray = faxNumbers.split(',');

Now to get unique numbers, sort the array and load only new ones into a
new array (the numbers are sorted alphabetically, but since they are all
numbers that shouldn't matter here):

faxNumArray.sort();
var faxNumUnique = [faxNumArray[0]];
for (var i=1, j=0, len=faxNumArray.length; i<len; ++i){
if (faxNumArray != faxNumUnique[j]){
faxNumUnique[++j] = faxNumArray;
}
}
// faxNumUnique now contains a sorted, unique list of fax numbers

Grateful if you could kindly advise whether step 2 is the best way to sort
out duplicated numbers?

No, it's not. The best way would be to use a UNIX sort unique, but
since this is JavaScript ... see above.
 
A

ASM

Tony WONG a écrit :
i have a number of forms with fax numbers to come up into arrays and then
combine to string.

after that i design the flow
1. break the string to array
now the string looks like this 12345678,23456789,34567890...
2. check record-2 again record-1,
check record-3 again record-2 & record-1
check record-4 again record-3 & record-2 & record-1
and so on... (if duplicated, drop it)

problem - step 1 break the string to array
var CDEF = new Array(faxnumbers);

CDEF = faxnumbers.split(',');
alert(CDEF[0],

would be 12345678
 
T

Tony WONG

Thanks a lot.


RobG said:
Tony said:
i have a number of forms with fax numbers to come up into arrays and then
combine to string.

after that i design the flow
1. break the string to array
now the string looks like this 12345678,23456789,34567890...
2. check record-2 again record-1,
check record-3 again record-2 & record-1
check record-4 again record-3 & record-2 & record-1
and so on... (if duplicated, drop it)

problem - step 1 break the string to array
var CDEF = new Array(faxnumbers);
alert(CDEF[0],

it returns all the numbers (12345678,23456789,34567890...

i just wish to have 12345678

anything wrong with my script?

Yes, it's very inefficient. If you start with a comma-delimited set of
numbers, you can split it into an array using split:

var faxNumbers = '1234,2345,3456,45646,...';
var faxNumArray = faxNumbers.split(',');

Now to get unique numbers, sort the array and load only new ones into a
new array (the numbers are sorted alphabetically, but since they are all
numbers that shouldn't matter here):

faxNumArray.sort();
var faxNumUnique = [faxNumArray[0]];
for (var i=1, j=0, len=faxNumArray.length; i<len; ++i){
if (faxNumArray != faxNumUnique[j]){
faxNumUnique[++j] = faxNumArray;
}
}
// faxNumUnique now contains a sorted, unique list of fax numbers

Grateful if you could kindly advise whether step 2 is the best way to
sort out duplicated numbers?

No, it's not. The best way would be to use a UNIX sort unique, but since
this is JavaScript ... see above.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Wed,
10 May 2006 07:34:16 remote, seen in RobG
Yes, it's very inefficient. If you start with a comma-delimited set of
numbers, you can split it into an array using split:

var faxNumbers = '1234,2345,3456,45646,...';
var faxNumArray = faxNumbers.split(',');

Now to get unique numbers, sort the array and load only new ones into a
new array (the numbers are sorted alphabetically, but since they are all
numbers that shouldn't matter here):

In sorting to determine duplicates, the sort order does not matter, and
hence there is no relevance in their being numbers. The only need is
that identical values sort to adjacent and that comparison of a given
pair is consistent.

The obvious javascript way is to do the built-in sort, then to make a
single pass removing repeats or copying non-repeats.

If the number of duplicates is large, however, it may be better to code
the sort explicitly using a reasonably efficient algorithm, but to then
modify it so that when two items are found to be equal one of them is
dropped.

OTOH, perhaps the most efficient sorts are non-stable (i.e. items
testing as identical do not have their order preserved) and do not use
comparison-for-equality.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top