counting duplicate values

Discussion in 'ASP General' started by NiQ, Feb 6, 2004.

  1. NiQ

    NiQ Guest

    Hello every one,
    this is about an array of string and may be have done several time
    before but i need it and wasnt able to find it so here is the problem

    i have an array of strings with contains words and i want a distinct
    list of array with the count of each number repeat i.e how many time
    it exist in the array and the final result should be sorted according
    to the number of repeat from hight to low .
    for example

    thearray="32a,7s,5a,29s,20,29s,32a,8,5a,32a,32a,32a"

    the result should be
    32a
    29s
    5a
    7s
    20
    8


    i have this code which take out all the duplicate values and print the
    distinct values

    function RemCDups(byVal thearray)

    Dim element,element2,k,i,j
    dim newArray()
    redim newArray(0)

    for i = 0 to ubound(thearray)

    element = thearray(i)
    element2 = "false"

    for j = i + 1 to Ubound(thearray)
    if theArray(j) = element then
    element2 = "true"
    end if
    next

    if element2 = "false" then
    redim preserve newArray(Ubound(newArray)+1)
    newArray(k) = theArray(i)
    k = k + 1
    end if
    next

    RemCDups = newArray
    end function

    but i want to modify it to do the sort according to the count of each
    element from high to low

    thanks.

    regards
    NIQ
     
    NiQ, Feb 6, 2004
    #1
    1. Advertising

  2. NiQ

    Roy Danon Guest

    So you have succeded finding the values and putting 'em into an array and
    now you want to order the array?

    for j = 1 to ubound(array)
    for i = 0 to ubound(array)
    if not i=ubound(array)
    if array(i)<array(i+1) then
    temp=array(i)
    array(i)=array(i+1)
    array(i+1)=temp
    end if
    end if
    next
    next

    I hope i've written the algorithem currect... this is the simplest way to
    order an array.
    you can also use JS.


    Roy.

    "NiQ" <> wrote in message
    news:...
    > Hello every one,
    > this is about an array of string and may be have done several time
    > before but i need it and wasnt able to find it so here is the problem
    >
    > i have an array of strings with contains words and i want a distinct
    > list of array with the count of each number repeat i.e how many time
    > it exist in the array and the final result should be sorted according
    > to the number of repeat from hight to low .
    > for example
    >
    > thearray="32a,7s,5a,29s,20,29s,32a,8,5a,32a,32a,32a"
    >
    > the result should be
    > 32a
    > 29s
    > 5a
    > 7s
    > 20
    > 8
    >
    >
    > i have this code which take out all the duplicate values and print the
    > distinct values
    >
    > function RemCDups(byVal thearray)
    >
    > Dim element,element2,k,i,j
    > dim newArray()
    > redim newArray(0)
    >
    > for i = 0 to ubound(thearray)
    >
    > element = thearray(i)
    > element2 = "false"
    >
    > for j = i + 1 to Ubound(thearray)
    > if theArray(j) = element then
    > element2 = "true"
    > end if
    > next
    >
    > if element2 = "false" then
    > redim preserve newArray(Ubound(newArray)+1)
    > newArray(k) = theArray(i)
    > k = k + 1
    > end if
    > next
    >
    > RemCDups = newArray
    > end function
    >
    > but i want to modify it to do the sort according to the count of each
    > element from high to low
    >
    > thanks.
    >
    > regards
    > NIQ
     
    Roy Danon, Feb 6, 2004
    #2
    1. Advertising

  3. NiQ

    Roland Hall Guest

    "NiQ" wrote:
    : i have an array of strings with contains words and i want a distinct
    : list of array with the count of each number repeat i.e how many time
    : it exist in the array and the final result should be sorted according
    : to the number of repeat from hight to low .
    : for example
    :
    : thearray="32a,7s,5a,29s,20,29s,32a,8,5a,32a,32a,32a"
    : the result should be
    : 32a
    : 29s
    : 5a
    : 7s
    : 20
    : 8
    : but i want to modify it to do the sort according to the count of each
    : element from high to low

    NiQ...

    I took a different approach and chose to use a dictionary.

    I took your string and split it to create an array of elements.

    I took the elements of the array and created a dictionary but only to add
    unique keys so the result was 6 keys. I then created another dictionary,
    comparing with the first dictionary keys to find the duplicates and then
    incremented the value of the items. After that I used a dictionary sort I
    found at Microsoft and modified it so I could pass the order to return the
    results.

    This is my solution. There is a link on the page to view the source.

    http://kiddanger.com/lab/array.asp

    HTH...

    --
    Roland Hall
    /* This information is distributed in the hope that it will be useful, but
    without any warranty; without even the implied warranty of merchantability
    or fitness for a particular purpose. */
    Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
    WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
    MSDN Library - http://msdn.microsoft.com/library/default.asp
     
    Roland Hall, Feb 7, 2004
    #3
  4. NiQ

    NiQ Guest

    thanks a lot to both of you . i will try these codes and will post
    incase of any furthur help . but i really appreciate this thanks a
    lot.

    regards
    niq

    "Roland Hall" <nobody@nowhere> wrote in message news:<>...
    > "NiQ" wrote:
    > : i have an array of strings with contains words and i want a distinct
    > : list of array with the count of each number repeat i.e how many time
    > : it exist in the array and the final result should be sorted according
    > : to the number of repeat from hight to low .
    > : for example
    > :
    > : thearray="32a,7s,5a,29s,20,29s,32a,8,5a,32a,32a,32a"
    > : the result should be
    > : 32a
    > : 29s
    > : 5a
    > : 7s
    > : 20
    > : 8
    > : but i want to modify it to do the sort according to the count of each
    > : element from high to low
    >
    > NiQ...
    >
    > I took a different approach and chose to use a dictionary.
    >
    > I took your string and split it to create an array of elements.
    >
    > I took the elements of the array and created a dictionary but only to add
    > unique keys so the result was 6 keys. I then created another dictionary,
    > comparing with the first dictionary keys to find the duplicates and then
    > incremented the value of the items. After that I used a dictionary sort I
    > found at Microsoft and modified it so I could pass the order to return the
    > results.
    >
    > This is my solution. There is a link on the page to view the source.
    >
    > http://kiddanger.com/lab/array.asp
    >
    > HTH...
     
    NiQ, Feb 9, 2004
    #4
  5. NiQ

    NiQ Guest

    Re: a little problem .

    hi ronald
    thanks again for your great help , i am just having a little problem
    when i increase the number of one exisitence more than 10 then it
    sorts it verz strangly i want to sort on the number of occourences
    with the code u wrote can u try this array
    thearray = "32a,7s,5a,29s,20,29s,32a,8,5a,32a,32a,32a,32a,32a,32a,32a,32a,32a,32a,32a"

    the result will be something like this
    2=29s
    2=5a
    13=32a
    1=8
    1=20
    1=7s

    when it should be
    13=32a
    2=29s
    2=5a
    1=8
    1=20
    1=7s

    please let me know how i can fix it .

    best regards
    NIQ

    "Roland Hall" <nobody@nowhere> wrote in message news:<>...
    > "NiQ" wrote:
    > : i have an array of strings with contains words and i want a distinct
    > : list of array with the count of each number repeat i.e how many time
    > : it exist in the array and the final result should be sorted according
    > : to the number of repeat from hight to low .
    > : for example
    > :
    > : thearray="32a,7s,5a,29s,20,29s,32a,8,5a,32a,32a,32a"
    > : the result should be
    > : 32a
    > : 29s
    > : 5a
    > : 7s
    > : 20
    > : 8
    > : but i want to modify it to do the sort according to the count of each
    > : element from high to low
    >
    > NiQ...
    >
    > I took a different approach and chose to use a dictionary.
    >
    > I took your string and split it to create an array of elements.
    >
    > I took the elements of the array and created a dictionary but only to add
    > unique keys so the result was 6 keys. I then created another dictionary,
    > comparing with the first dictionary keys to find the duplicates and then
    > incremented the value of the items. After that I used a dictionary sort I
    > found at Microsoft and modified it so I could pass the order to return the
    > results.
    >
    > This is my solution. There is a link on the page to view the source.
    >
    > http://kiddanger.com/lab/array.asp
    >
    > HTH...
     
    NiQ, Feb 9, 2004
    #5
  6. NiQ

    Bob Barrows Guest

    Re: a little problem .

    NiQ wrote:
    > hi ronald
    > thanks again for your great help , i am just having a little problem
    > when i increase the number of one exisitence more than 10 then it
    > sorts it verz strangly i want to sort on the number of occourences
    > with the code u wrote can u try this array
    > thearray =
    >

    "32a,7s,5a,29s,20,29s,32a,8,5a,32a,32a,32a,32a,32a,32a,32a,32a,32a,32a,32a"
    >
    > the result will be something like this
    > 2=29s
    > 2=5a
    > 13=32a
    > 1=8
    > 1=20
    > 1=7s


    This is the correct alpha (string) sort order.
    >
    > when it should be
    > 13=32a
    > 2=29s
    > 2=5a
    > 1=8
    > 1=20
    > 1=7s
    >



    You want a numeric sort order so you need to convert the strings to numbers
    before doing the sort. This is the line that needs changing:

    If StrComp(strDict(X,intSort),strDict(Y,intSort),vbTextCompare) > 0 Then

    Change it to
    If CInt(strDict(X,intSort)) > CInt(strDict(Y,intSort)) Then

    HTH,
    Bob Barrows

    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"
     
    Bob Barrows, Feb 9, 2004
    #6
  7. Re: a little problem .

    Roland Hall wrote:
    >
    > I changed the whole function so you can pass text or numeric so the
    > sort routine could be reusable in the future.
    > http://kiddanger.com/lab/array.asp
    >
    > Thanks for your help Bob. (O:=


    :)
    What if a date comparison is needed ... ? Or Doubles?

    Bob Barrows

    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
     
    Bob Barrows [MVP], Feb 9, 2004
    #7
  8. NiQ

    Roland Hall Guest

    Re: a little problem .

    "Bob Barrows" wrote:
    : NiQ wrote:
    : > hi ronald
    : > thanks again for your great help , i am just having a little problem
    : > when i increase the number of one exisitence more than 10 then it
    : > sorts it verz strangly i want to sort on the number of occourences
    : > with the code u wrote can u try this array
    : > thearray =
    : >
    :
    "32a,7s,5a,29s,20,29s,32a,8,5a,32a,32a,32a,32a,32a,32a,32a,32a,32a,32a,32a"
    : >
    : > the result will be something like this
    : > 2=29s
    : > 2=5a
    : > 13=32a
    : > 1=8
    : > 1=20
    : > 1=7s
    :
    : This is the correct alpha (string) sort order.
    : >
    : > when it should be
    : > 13=32a
    : > 2=29s
    : > 2=5a
    : > 1=8
    : > 1=20
    : > 1=7s
    : >
    :
    :
    : You want a numeric sort order so you need to convert the strings to
    numbers
    : before doing the sort. This is the line that needs changing:
    :
    : If StrComp(strDict(X,intSort),strDict(Y,intSort),vbTextCompare) > 0 Then
    :
    : Change it to
    : If CInt(strDict(X,intSort)) > CInt(strDict(Y,intSort)) Then

    I changed the whole function so you can pass text or numeric so the sort
    routine could be reusable in the future.
    http://kiddanger.com/lab/array.asp

    Thanks for your help Bob. (O:=

    --
    Roland Hall
    /* This information is distributed in the hope that it will be useful, but
    without any warranty; without even the implied warranty of merchantability
    or fitness for a particular purpose. */
    Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
    WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
    MSDN Library - http://msdn.microsoft.com/library/default.asp
     
    Roland Hall, Feb 9, 2004
    #8
  9. NiQ

    NiQ Guest

    hi Roland,
    there is something wrong . it take the count sort as character i.e if
    the

    occourance is 102 then it will come with all 1's then all 2's

    thearray="32a,7s,5a,29s,20,29s,32a,8,5a,32a,32a,32a,32a,32a,32a,32a,32a,32a,32a,32a,32a"

    try with this and you will see what i mean .

    i tried a little but can get it work . may be you can help

    thanks a lot

    regards
    niq

    (NiQ) wrote in message news:<>...
    > thanks a lot to both of you . i will try these codes and will post
    > incase of any furthur help . but i really appreciate this thanks a
    > lot.
    >
    > regards
    > niq
    >
    > "Roland Hall" <nobody@nowhere> wrote in message news:<>...
    > > "NiQ" wrote:
    > > : i have an array of strings with contains words and i want a distinct
    > > : list of array with the count of each number repeat i.e how many time
    > > : it exist in the array and the final result should be sorted according
    > > : to the number of repeat from hight to low .
    > > : for example
    > > :
    > > : thearray="32a,7s,5a,29s,20,29s,32a,8,5a,32a,32a,32a"
    > > : the result should be
    > > : 32a
    > > : 29s
    > > : 5a
    > > : 7s
    > > : 20
    > > : 8
    > > : but i want to modify it to do the sort according to the count of each
    > > : element from high to low
    > >
    > > NiQ...
    > >
    > > I took a different approach and chose to use a dictionary.
    > >
    > > I took your string and split it to create an array of elements.
    > >
    > > I took the elements of the array and created a dictionary but only to add
    > > unique keys so the result was 6 keys. I then created another dictionary,
    > > comparing with the first dictionary keys to find the duplicates and then
    > > incremented the value of the items. After that I used a dictionary sort I
    > > found at Microsoft and modified it so I could pass the order to return the
    > > results.
    > >
    > > This is my solution. There is a link on the page to view the source.
    > >
    > > http://kiddanger.com/lab/array.asp
    > >
    > > HTH...
     
    NiQ, Feb 11, 2004
    #9
  10. NiQ

    Roland Hall Guest

    "NiQ" wrote:
    : hi Roland,
    : there is something wrong . it take the count sort as character i.e if
    : the
    :
    : occourance is 102 then it will come with all 1's then all 2's
    :
    :
    thearray="32a,7s,5a,29s,20,29s,32a,8,5a,32a,32a,32a,32a,32a,32a,32a,32a,32a,
    32a,32a,32a"
    :
    : try with this and you will see what i mean .
    :
    : i tried a little but can get it work . may be you can help

    I did and it works fine. Perhaps you missed my other post following Bob's
    first post on this thread. The example is running with the above value.

    --
    Roland Hall
    /* This information is distributed in the hope that it will be useful, but
    without any warranty; without even the implied warranty of merchantability
    or fitness for a particular purpose. */
    Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
    WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
    MSDN Library - http://msdn.microsoft.com/library/default.asp
     
    Roland Hall, Feb 11, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dotpro2008

    How to prevent storing duplicate values

    dotpro2008, Feb 15, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    443
    Misbah Arefin
    Feb 15, 2008
  2. curiousEngine
    Replies:
    0
    Views:
    404
    curiousEngine
    Apr 11, 2008
  3. Sanjeev
    Replies:
    0
    Views:
    432
    Sanjeev
    Jun 23, 2008
  4. Sanjeev
    Replies:
    9
    Views:
    719
  5. edwardfredriks

    counting up instead of counting down

    edwardfredriks, Sep 6, 2005, in forum: Javascript
    Replies:
    6
    Views:
    209
    Dr John Stockton
    Sep 7, 2005
Loading...

Share This Page