identify duplicates in an array and number of times duplicated

Discussion in 'ASP General' started by Michelle, Aug 1, 2003.

  1. Michelle

    Michelle Guest

    hi,

    i have created an array from recordset containing user names
    eg. (davidp, davidp, evenf, patricka, rebeccah)

    which i have sorted in alphabetical order, but i need to
    identify duplicates in this array and the number of times it has
    been duplicated.

    can someone help?

    --
    Michelle
    Michelle, Aug 1, 2003
    #1
    1. Advertising

  2. Michelle

    Ray at Guest

    If you've already sorted your array alphabetically, you can do:

    Dim sCurVal, sLastVal, iDups
    iDups = 0
    For i = LBound(YourArray) To UBound(YourArray)
    sCurVal = YourArray(i)
    If sCurVal = sLastVal Then iDups = iDups + 1
    sLastVal = sCurVal
    Next


    This will just give you the total number of duplicate items, like, if your
    array is

    a,b,c,c,d,e,e,e,f,g

    iDups will return as 3, since there is one extra c and two extra e's.

    Is that what you wanted, or did you want to be able to say:
    c has 1 duplicate
    e has two duplicates
    etc.

    Ray at work





    "Michelle" <> wrote in message
    news:3f2a7499$0$10354$...
    > hi,
    >
    > i have created an array from recordset containing user names
    > eg. (davidp, davidp, evenf, patricka, rebeccah)
    >
    > which i have sorted in alphabetical order, but i need to
    > identify duplicates in this array and the number of times it has
    > been duplicated.
    >
    > can someone help?
    >
    > --
    > Michelle
    >
    >
    >
    Ray at, Aug 1, 2003
    #2
    1. Advertising

  3. Michelle

    dlbjr Guest

    Why not do a count in the SQL query string and get the username and count from the recordset?



    -------------------------------------------------
    d l b j r

    Unambit from meager knowledge of inane others,
    engender uncharted sagacity.
    -------------------------------------------------
    dlbjr, Aug 1, 2003
    #3
  4. Michelle

    Chris Barber Guest

    You'd be better off getting another recordset with the duplicate counts
    already created for you?

    SELECT [Name] as UserName, Count([Name]) as CountOfUserName
    FROM YourTable
    GROUP BY [Name]
    ORDER BY Count([Name]) DESC

    This will give you:

    UserName CountOfUserName
    Chris 10
    Dave 7
    Henrik 2
    Michale 1
    Joan 1

    etc.

    If you only want the duplicates listed then change it to be:

    SELECT [Name] as UserName, Count([Name]) as CountOfUserName
    FROM YourTable
    WHERE Count([Name]) > 1
    GROUP BY [Name]
    ORDER BY Count([Name]) DESC

    Hope this helps.

    Chris.

    "Michelle" <> wrote in message
    news:3f2a7499$0$10354$...
    hi,

    i have created an array from recordset containing user names
    eg. (davidp, davidp, evenf, patricka, rebeccah)

    which i have sorted in alphabetical order, but i need to
    identify duplicates in this array and the number of times it has
    been duplicated.

    can someone help?

    --
    Michelle
    Chris Barber, Aug 2, 2003
    #4
  5. Michelle

    Michelle Guest

    Hi Ray

    > Is that what you wanted, or did you want to be able to say:
    > c has 1 duplicate
    > e has two duplicates


    Yes this is what i want

    I tried to use your code, although i had to add 'end if', it gave me a
    a 'Subscript out of range' error at
    >sCurVal = YourArray(i)


    ----------------------------------------------
    this is my code for the array
    ----------------------------------------------
    <%
    Dim MyArray, sOutput
    MyArray = rs.GetRows()
    MyArray = arraysort(MyArray)

    Dim iRowLoop
    For iRowLoop = 0 to UBound(MyArray, 2)
    sOutput = trim(left(MyArray(iColLoop, iRowLoop),10)) & "<br> "
    Response.Write(sOutput)
    Next
    %>
    ----------------------------------------------

    Actually the rows in the array is unique, the source is a text file dump of
    terminal service processes, but i need to extract the user names of those
    using a particular process twice, and how many times they have these
    processes open. I cannot sort or search for duplicates in SQL, because the
    first line contains a lot of spaces inbetween the headings which dosnt make
    a qualified column name.

    Hope you can help.

    --
    Michelle


    "Ray at <%=sLocation%>" <myfirstname at lane34 dot com> wrote in message
    news:#...
    > If you've already sorted your array alphabetically, you can do:
    >
    > Dim sCurVal, sLastVal, iDups
    > iDups = 0
    > For i = LBound(YourArray) To UBound(YourArray)
    > sCurVal = YourArray(i)
    > If sCurVal = sLastVal Then iDups = iDups + 1
    > sLastVal = sCurVal
    > Next
    >
    >
    > This will just give you the total number of duplicate items, like, if your
    > array is
    >
    > a,b,c,c,d,e,e,e,f,g
    >
    > iDups will return as 3, since there is one extra c and two extra e's.
    >
    > Is that what you wanted, or did you want to be able to say:
    > c has 1 duplicate
    > e has two duplicates
    > etc.
    >
    > Ray at work
    >
    >
    >
    >
    >
    > "Michelle" <> wrote in message
    > news:3f2a7499$0$10354$...
    > > hi,
    > >
    > > i have created an array from recordset containing user names
    > > eg. (davidp, davidp, evenf, patricka, rebeccah)
    > >
    > > which i have sorted in alphabetical order, but i need to
    > > identify duplicates in this array and the number of times it has
    > > been duplicated.
    > >
    > > can someone help?
    > >
    > > --
    > > Michelle
    > >
    > >
    > >

    >
    >
    Michelle, Aug 2, 2003
    #5
  6. Michelle

    Chris Barber Guest

    Re:
    I cannot sort or search for duplicates in SQL, because the
    first line contains a lot of spaces in-between the headings which doesn't
    make
    a qualified column name.

    That doesn't seem to make sense - in order to get a recordset you must be
    specifying some SQL? You can determine the 'real' field names by enumerating
    the fields collection or just use the numeric field indexes 0 to however
    many fields.

    eg. SELECT TOP 1 * FROM Table

    will get a single row recordset that you can look at to get the field names.

    However, you know what you have so I'll stop there and let you decide what's
    easiest to implement (array manipulation or SQL).

    Chris.

    "Michelle" <> wrote in message
    news:3f2b1843$0$10355$...
    Please see reply to Ray

    --
    Michelle



    "Chris Barber" <> wrote in message
    news:...
    > You'd be better off getting another recordset with the duplicate counts
    > already created for you?
    >
    > SELECT [Name] as UserName, Count([Name]) as CountOfUserName
    > FROM YourTable
    > GROUP BY [Name]
    > ORDER BY Count([Name]) DESC
    >
    > This will give you:
    >
    > UserName CountOfUserName
    > Chris 10
    > Dave 7
    > Henrik 2
    > Michale 1
    > Joan 1
    >
    > etc.
    >
    > If you only want the duplicates listed then change it to be:
    >
    > SELECT [Name] as UserName, Count([Name]) as CountOfUserName
    > FROM YourTable
    > WHERE Count([Name]) > 1
    > GROUP BY [Name]
    > ORDER BY Count([Name]) DESC
    >
    > Hope this helps.
    >
    > Chris.
    >
    > "Michelle" <> wrote in message
    > news:3f2a7499$0$10354$...
    > hi,
    >
    > i have created an array from recordset containing user names
    > eg. (davidp, davidp, evenf, patricka, rebeccah)
    >
    > which i have sorted in alphabetical order, but i need to
    > identify duplicates in this array and the number of times it has
    > been duplicated.
    >
    > can someone help?
    >
    > --
    > Michelle
    >
    >
    >
    >
    Chris Barber, Aug 2, 2003
    #6
  7. Michelle

    Michelle Guest

    Hi Chris

    This is the SQL i am using
    > sql="SELECT * from process.txt"


    process.txt is a text file dumped from ms-dos and this is the first 6 lines
    from this file
    --------------------------------------------------------
    USERNAME SESSIONNAME ID PID IMAGE
    davidp id 1 1 3472 notepad.exe
    davidp id 9 9 4348 notepad.exe
    evenf id 8 8 5160 notepad.exe
    patricka id 19 19 7904 notepad.exe
    rebeccah id 10 10 2820 notepad.exe
    --------------------------------------------------------
    since this file isnt delimited, all this data is retrieved into one column
    and the first line contains spaces, so i cannot do

    sql="SELECT 'USERNAME SESSIONNAME ID PID IMAGE'
    from process.txt"
    or any other sql statement that requires i directly name the column

    >You can determine the 'real' field names by enumerating
    > the fields collection or just use the numeric field indexes 0 to however

    how can i do this in ASP?

    I want to be able to show that davidp is using a program twice
    eg. davidp (2)

    --
    Michelle


    "Chris Barber" <> wrote in message
    news:...
    > Re:
    > I cannot sort or search for duplicates in SQL, because the
    > first line contains a lot of spaces in-between the headings which doesn't
    > make
    > a qualified column name.
    >
    > That doesn't seem to make sense - in order to get a recordset you must be
    > specifying some SQL? You can determine the 'real' field names by

    enumerating
    > the fields collection or just use the numeric field indexes 0 to however
    > many fields.
    >
    > eg. SELECT TOP 1 * FROM Table
    >
    > will get a single row recordset that you can look at to get the field

    names.
    >
    > However, you know what you have so I'll stop there and let you decide

    what's
    > easiest to implement (array manipulation or SQL).
    >
    > Chris.
    >
    > "Michelle" <> wrote in message
    > news:3f2b1843$0$10355$...
    > Please see reply to Ray
    >
    > --
    > Michelle
    >
    >
    >
    > "Chris Barber" <> wrote in message
    > news:...
    > > You'd be better off getting another recordset with the duplicate counts
    > > already created for you?
    > >
    > > SELECT [Name] as UserName, Count([Name]) as CountOfUserName
    > > FROM YourTable
    > > GROUP BY [Name]
    > > ORDER BY Count([Name]) DESC
    > >
    > > This will give you:
    > >
    > > UserName CountOfUserName
    > > Chris 10
    > > Dave 7
    > > Henrik 2
    > > Michale 1
    > > Joan 1
    > >
    > > etc.
    > >
    > > If you only want the duplicates listed then change it to be:
    > >
    > > SELECT [Name] as UserName, Count([Name]) as CountOfUserName
    > > FROM YourTable
    > > WHERE Count([Name]) > 1
    > > GROUP BY [Name]
    > > ORDER BY Count([Name]) DESC
    > >
    > > Hope this helps.
    > >
    > > Chris.
    > >
    > > "Michelle" <> wrote in message
    > > news:3f2a7499$0$10354$...
    > > hi,
    > >
    > > i have created an array from recordset containing user names
    > > eg. (davidp, davidp, evenf, patricka, rebeccah)
    > >
    > > which i have sorted in alphabetical order, but i need to
    > > identify duplicates in this array and the number of times it has
    > > been duplicated.
    > >
    > > can someone help?
    > >
    > > --
    > > Michelle
    > >
    > >
    > >
    > >

    >
    >
    >
    Michelle, Aug 2, 2003
    #7
  8. Michelle

    Michelle Guest

    Hi All

    Thank you for your inputs, but i have solved my problem,
    i didnt ask the right question in the beginning, what i needed was
    to search a string for duplicates and the number of times the value
    had been duplicated.

    But what i had was, an array of unique values, which i manipulated
    and displayed to show some duplicates.

    Thanks again.
    --
    Michelle


    "Michelle" <> wrote in message
    news:3f2a7499$0$10354$...
    > hi,
    >
    > i have created an array from recordset containing user names
    > eg. (davidp, davidp, evenf, patricka, rebeccah)
    >
    > which i have sorted in alphabetical order, but i need to
    > identify duplicates in this array and the number of times it has
    > been duplicated.
    >
    > can someone help?
    >
    > --
    > Michelle
    >
    >
    >
    Michelle, Aug 2, 2003
    #8
  9. Michelle

    Chris Barber Guest

    Perhaps this is space or fixed width delimited?
    It is possible to query a text file that is space or fixed width delimited
    by column names but I'm pretty sure you're already aware of that and may
    even have tried it.

    Cheers,

    Chris.

    "Michelle" <> wrote in message
    news:3f2b276e$0$10357$...
    Hi Chris

    This is the SQL i am using
    > sql="SELECT * from process.txt"


    process.txt is a text file dumped from ms-dos and this is the first 6 lines
    from this file
    --------------------------------------------------------
    USERNAME SESSIONNAME ID PID IMAGE
    davidp id 1 1 3472 notepad.exe
    davidp id 9 9 4348 notepad.exe
    evenf id 8 8 5160 notepad.exe
    patricka id 19 19 7904 notepad.exe
    rebeccah id 10 10 2820 notepad.exe
    --------------------------------------------------------
    since this file isnt delimited, all this data is retrieved into one column
    and the first line contains spaces, so i cannot do

    sql="SELECT 'USERNAME SESSIONNAME ID PID IMAGE'
    from process.txt"
    or any other sql statement that requires i directly name the column

    >You can determine the 'real' field names by enumerating
    > the fields collection or just use the numeric field indexes 0 to however

    how can i do this in ASP?

    I want to be able to show that davidp is using a program twice
    eg. davidp (2)

    --
    Michelle


    "Chris Barber" <> wrote in message
    news:...
    > Re:
    > I cannot sort or search for duplicates in SQL, because the
    > first line contains a lot of spaces in-between the headings which doesn't
    > make
    > a qualified column name.
    >
    > That doesn't seem to make sense - in order to get a recordset you must be
    > specifying some SQL? You can determine the 'real' field names by

    enumerating
    > the fields collection or just use the numeric field indexes 0 to however
    > many fields.
    >
    > eg. SELECT TOP 1 * FROM Table
    >
    > will get a single row recordset that you can look at to get the field

    names.
    >
    > However, you know what you have so I'll stop there and let you decide

    what's
    > easiest to implement (array manipulation or SQL).
    >
    > Chris.
    >
    > "Michelle" <> wrote in message
    > news:3f2b1843$0$10355$...
    > Please see reply to Ray
    >
    > --
    > Michelle
    >
    >
    >
    > "Chris Barber" <> wrote in message
    > news:...
    > > You'd be better off getting another recordset with the duplicate counts
    > > already created for you?
    > >
    > > SELECT [Name] as UserName, Count([Name]) as CountOfUserName
    > > FROM YourTable
    > > GROUP BY [Name]
    > > ORDER BY Count([Name]) DESC
    > >
    > > This will give you:
    > >
    > > UserName CountOfUserName
    > > Chris 10
    > > Dave 7
    > > Henrik 2
    > > Michale 1
    > > Joan 1
    > >
    > > etc.
    > >
    > > If you only want the duplicates listed then change it to be:
    > >
    > > SELECT [Name] as UserName, Count([Name]) as CountOfUserName
    > > FROM YourTable
    > > WHERE Count([Name]) > 1
    > > GROUP BY [Name]
    > > ORDER BY Count([Name]) DESC
    > >
    > > Hope this helps.
    > >
    > > Chris.
    > >
    > > "Michelle" <> wrote in message
    > > news:3f2a7499$0$10354$...
    > > hi,
    > >
    > > i have created an array from recordset containing user names
    > > eg. (davidp, davidp, evenf, patricka, rebeccah)
    > >
    > > which i have sorted in alphabetical order, but i need to
    > > identify duplicates in this array and the number of times it has
    > > been duplicated.
    > >
    > > can someone help?
    > >
    > > --
    > > Michelle
    > >
    > >
    > >
    > >

    >
    >
    >
    Chris Barber, Aug 2, 2003
    #9
    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. =?Utf-8?B?bWF2cmlja18xMDE=?=

    SetAuthCookie works some times and fails some times?

    =?Utf-8?B?bWF2cmlja18xMDE=?=, Mar 23, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    497
    =?Utf-8?B?bWF2cmlja18xMDE=?=
    Mar 23, 2006
  2. Replies:
    4
    Views:
    294
    Neil Cerutti
    Dec 7, 2005
  3. Cyrus
    Replies:
    1
    Views:
    80
    A. Sinan Unur
    Dec 20, 2006
  4. Cyrus
    Replies:
    19
    Views:
    176
    Cyrus
    Dec 22, 2006
  5. Cyrus
    Replies:
    1
    Views:
    72
    A. Sinan Unur
    Dec 20, 2006
Loading...

Share This Page