+ Reply to Thread
Results 1 to 36 of 36

Remove duplicates and sort array

  1. #1
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Remove duplicates and sort array

    Hi,

    I have a one dimensional array and I want to remove all duplicates from the array so that each value in the array is unique. Then I need to sort all the 'unique' values in the array. What is the most efficient way to accomplish these tasks?

    Thanks.

    abousetta
    Last edited by abousetta; 02-24-2012 at 02:19 AM.
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Most efficient way to remove duplicates and sort array

    Hi,

    Probably the simplest way is to use Data Filter Advanced Unique and copy to another range.
    Then sort the unique range.
    Delete your original array and cut and paste the unique range back to the top of the original array.

    Regards
    Regards
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Most efficient way to remove duplicates and sort array

    Hi Richard,

    Sorry I should have been more clear about the source of my array. It is a string converted into an array, actually two strings.

    Please Login or Register  to view this content.
    What I am doing is comparing the two strings for similarities but there is no reason to go through repeated words since they will either be found or not.

    abousetta

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Most efficient way to remove duplicates and sort array

    Hi,

    Does Chip Pearson's article http://www.cpearson.com/excel/distinctvalues.aspx help.

    I'm sure what you want is in there somewhere.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Most efficient way to remove duplicates and sort array

    Hello Ahmed,

    I think a better approach would to be to use a Dictionary object. You can easily create a 1-D array of unique values. You could also do this with a Collection object. I prefer the Dictionary object as you can test if an element already exists without generating an error. If you need help coding this, let me know.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  6. #6
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Most efficient way to remove duplicates and sort array

    Hi Leith,

    Hope all is well. An example would be great. I know that you posted some examples a while back when we were both responding to another thread where the OP wanted to remove duplicates from a large list, but I can't find the link to it.

    Thanks for all your help.

    abousetta

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Most efficient way to remove duplicates and sort array

    Hello Ahmed,

    Here is an example macro.
    Please Login or Register  to view this content.

  8. #8
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Remove duplicates and sort array

    Perfect. Thanks Leith. I am trying to go the Array way these days (no pun intended). Arrays are said to be faster and more efficient and I am trying to learn to code using them but still learning the ropes.

    abousetta

  9. #9
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Remove duplicates and sort array

    Hi Leith,

    I had to just make one small change to your code (Key as Variant instead of String), but it works great. Thanks a lot for your help.

    By any chance would you know how to solve the second half of the riddle (how to sort the array)? I am still searching but no obvious answer in sight.

    abousetta

  10. #10
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Remove duplicates and sort array

    Please Login or Register  to view this content.



  11. #11
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Remove duplicates and sort array

    Thanks snb. Unfortunately, I can't seem to get it to work. The array seems to be empty (or contains null values) because even the Msgbox is not showing me anything within the newly created sn array. I will keep testing it and hopefully get it going.

    abousetta

  12. #12
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Remove duplicates and sort array

    Something has to be in S1 and S2.
    If not than use:

    Please Login or Register  to view this content.

  13. #13
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Remove duplicates and sort array

    Sorry. Now I know what I was doing wrong. c01 is a string. Therefore I needed to convert it back to an array to see the elements of the array.

    I am going to mark this thread as solved for now and work on trying to sort the elements within the array.

    Thanks everyone.

    abousetta

  14. #14
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Remove duplicates and sort array

    and now the sorting part: resulting in a sorted array 'sp'

    Please Login or Register  to view this content.

  15. #15
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Remove duplicates and sort array

    Thanks snb. As usual, I will need a big mug of coffee to try and decipher the advanced coding techniques, but I can always say that I am gaining a great amount of experience studying your approaches.

    abousetta

  16. #16
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Remove duplicates and sort array

    I have modified the code that snb provided to compensate for the nature of the data, but have one final problem that I can't figure out.

    Please Login or Register  to view this content.
    Now this works fine on small passages (a couple of sentences), but larger paragraphs the results are not accurate with some duplicates still being present and some out of alpha-numeric order. The weird thing is that when I rerun it again, it works just fine giving accurate results. For now I have put in a loop to go through the deduplicate and sort process twice, but this of course is inefficient.

    Any thoughts?

    abousetta

  17. #17
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Remove duplicates and sort array

    As an option. There is a NoDups ZVI's function. Here's an example using this function.
    Please Login or Register  to view this content.

  18. #18
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Remove duplicates and sort array

    Thanks nilem. I will test tomorrow and post back. Just out of curiosity what's a ZVI function (never came across this term before).

  19. #19
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Remove duplicates and sort array

    If this is your goal:

    ' Remove first (empty) element from Array
    you should use
    CitationArray = Split(mid(NewString,2), "|")

    If you provide the string you are testing with we can have a look too.
    Last edited by snb; 02-24-2012 at 04:09 AM.

  20. #20
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Remove duplicates and sort array

    Quote Originally Posted by abousetta View Post
    ... Just out of curiosity what's a ZVI function (never came across this term before).
    ZVI - is the nickname of Vladimir Zakharov, superprofessional in programming, MVP Excel

  21. #21
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Remove duplicates and sort array

    Thanks nilem. I am unfortunately a fish out of water when it comes to Excel .

    In fact, I have heard of ZVI, but haven't ever studied his techniques... maybe its time to start.

    snb, I have uploaded a sample of ten rows that show the problem. In the first sheet, I ran the code for deduping and sorting once, while in the second sheet, I ran the code twice. As you can see, the second returns 100% for all three columns since they are all identical. All the code I used is in the upload (didn't strip it down since the problem may be lying somewhere else in the code that I don't see).

    Thanks.

    abousetta
    Attached Files Attached Files

  22. #22
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Remove duplicates and sort array

    nilem, the code works great! It does the deduping and sorting at the same time. I'm impressed

    Question though, what does the "&" sign after the variable mean?

    Please Login or Register  to view this content.
    Thanks again.

    abousetta

  23. #23
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Remove duplicates and sort array

    This is the abbreviation for types of variables:
    & means Long
    % Integer
    # Double
    $ String
    ! Single
    And I still give the original code of ZVI
    Please Login or Register  to view this content.
    There is a faster way to extract unique values with sorting.

  24. #24
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Remove duplicates and sort array

    Thanks nilem. I found this great thread and discussion between ZVI and pgc01. I am going to test the two three approaches (Instr, Dict, Collection). From what I have read, the resutls with the Dictionary approach should prove to be the fastest, but I want to see what the differences will be with my data sets.

  25. #25
    Registered User
    Join Date
    12-29-2011
    Location
    Waldeck, Canada
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Remove duplicates and sort array

    Hi nilem,
    Thanks for this, just a question about this line:
    arr = Intersect(rng.Parent.UsedRange, rng).Value

    Why not just:
    arr = rng.Value

    I must be missing something,

    Thanks,

    Dave

  26. #26
    Forum Contributor jimmalk's Avatar
    Join Date
    08-31-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    113

    Re: Remove duplicates and sort array

    My compliments to everyone on this thread. Great stuff. Thank you

  27. #27
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Remove duplicates and sort array

    Hi, Dave
    You can select the entire column, for example Set rng = Range("A:B"), and with an expression of arr = rng.Value you will have 1,048,576 * 2 elements in your array.
    But the expression of arr = Intersect(rng.Parent.UsedRange, rng).Value restricts the selected columns only used range on the sheet.

  28. #28
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Remove duplicates and sort array

    This does both in one routine

    Please Login or Register  to view this content.
    Last edited by mikerickson; 10-11-2012 at 01:52 AM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  29. #29
    Registered User
    Join Date
    12-29-2011
    Location
    Waldeck, Canada
    MS-Off Ver
    Excel 2010
    Posts
    4

    Smile Re: Remove duplicates and sort array

    Quote Originally Posted by nilem View Post
    Hi, Dave
    You can select the entire column, for example Set rng = Range("A:B"), and with an expression of arr = rng.Value you will have 1,048,576 * 2 elements in your array.
    But the expression of arr = Intersect(rng.Parent.UsedRange, rng).Value restricts the selected columns only used range on the sheet.
    Hi nilem,

    Oh yes, of course, I hadn't considered that possibility (entire columns). Thanks for the reply and great explanation.

    Regards,

    Dave

  30. #30
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Remove duplicates and sort array

    Thanks again nilem and Mike. Mike's approach in itself is ingenious as it opens up a whole new approach that I had never thought of, which is having a function keep calling itself (looping) until it completes the task. I used to think this can only be accomplished by a sub calling a function with Do... Loop while approach and Public variables.

    I'm going to mark this thread as solved for now and want to thank everyone for the wealth of knowledge in this discussion.

    abousetta

  31. #31
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Remove duplicates and sort array

    Quote Originally Posted by abousetta View Post
    Thanks again nilem and Mike. Mike's approach in itself is ingenious as it opens up a whole new approach that I had never thought of, which is having a function keep calling itself (looping) until it completes the task. I used to think this can only be accomplished by a sub calling a function with Do... Loop while approach and Public variables.

    I'm going to mark this thread as solved for now and want to thank everyone for the wealth of knowledge in this discussion.

    abousetta
    A function that calls itself is called a "recursive function".

  32. #32
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Remove duplicates and sort array

    Thanks Mike. I currently remove duplicates with the scripting dictionary and then sort using one of the standard vb methods (e.g. bubble sort, etc.).

  33. #33
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Remove duplicates and sort array

    Why not use one object to sort and remove duplicates? Consider:
    Please Login or Register  to view this content.
    Last edited by Kyle123; 10-11-2012 at 09:41 AM.

  34. #34
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Remove duplicates and sort array

    See the disadvantage of use of arraylist/sortedlist.
    Please Login or Register  to view this content.

  35. #35
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Remove duplicates and sort array

    True, but that's only a limitation of mixed types. For all strings or all numbers, it is perfectly valid - still something to bear in mind

  36. #36
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Remove duplicates and sort array

    Quote Originally Posted by Kyle123 View Post
    True, but that's only a limitation of mixed types. For all strings or all numbers, it is perfectly valid - still something to bear in mind
    That is critical....

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1