www.webdeveloper.com
Results 1 to 13 of 13

Thread: combine list remove dups

  1. #1
    Join Date
    Feb 2010
    Posts
    34

    combine list remove dups

    I have two list as following want to combine, but could not, please help.

    list1 = "a,b,c,d,e,f";
    list2 ='d,f,w,x,y";

    need it to be : a,b,c,d,e,f,w,x,y

    Thanks

  2. #2
    Join Date
    Oct 2007
    Location
    Western Massachusetts, USA
    Posts
    387
    Pretty easy 3 stages. Combine the two arrays, remove duplicates (except for 1 copy), then sort.
    PHP Code:
    var list1 "a,b,c,d,e,f";
    var 
    list2 "d,f,w,x,y";
    var 
    list1split list1.split(',');
    var 
    list2split list2.split(',');
    var 
    combined_raw = new Array();
    var 
    combined_unique = new Array();
    var 
    combined_final = new Array();
    var 
    i;
    var 
    x;

    //append list1split and list2split into combined_raw
      
    for (0list1split.lengthi++) { combined_raw[combined_raw.length] = list1split[i]; }
      for (
    0list2split.lengthi++) { combined_raw[combined_raw.length] = list2split[i]; }

    //turn combined_raw into combined_unique, omitting duplicates
      
    for (0combined_raw.lengthi++) {
      var 
    found false;
        for (
    0combined_unique.lengthx++) {
          if (
    combined_unique[x] == combined_raw[i]) { found true; break; }
        }
        if (!
    found) {
        
    combined_unique[combined_unique.length] = combined_raw[i];
        }
      }

    //sort combined_unique into combined_final
    combined_final combined_unique.sort();

    document.write("<p>\n");
    document.write("<p>list1split = "  list1split.sort() + "<br />\n");
    document.write("list2split = "     list2split.sort() + "<br />\n");
    document.write("combined_final = " combined_final    "<br />\n");
    document.write("</p>\n"); 
    Computer programmer / web developer.

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,391

    Lightbulb

    Try this:
    Code:
    <script type="text/javascript">
    // From: http://www.webdeveloper.com/forum/showthread.php?t=248156
    
    // Following from: http://stackoverflow.com/questions/1584370/how-to-merge-two-arrays-in-javascript
    Array.prototype.uniqueMerge = function( a ) {
        for ( var nonDuplicates = [], i = 0, l = a.length; i<l; ++i ) {
            if ( this.indexOf( a[i] ) === -1 ) {
                nonDuplicates.push( a[i] );
            }
        }
        return this.concat( nonDuplicates )
    };
    
    var list1 = ['a','b','c','d','e','f'];
    var list2 = ['d','f','w','x','y'];
    var list3 = [];
        list3 = list1.uniqueMerge(list2);
    
    alert(list1.join('\n'));
    alert(list2.join('\n'));
    alert(list3.join('\n'));
    
    </script>

  4. #4
    Join Date
    Feb 2010
    Posts
    34
    Thanks for the response. I like the solution, 2 questions if I may ask, how could I make it work for IE? it works for firefox ok but not for my IE browser. How could I use it if the two list values come from input textbox. Appreciate your help.

  5. #5
    Join Date
    Feb 2010
    Posts
    34
    Thanks for your help. This one works perfectly. Appreciate your help!
    Last edited by jt107; 06-28-2011 at 07:46 AM. Reason: Resolved

  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,391

    Thumbs up

    Quote Originally Posted by jt107 View Post
    Thanks for the response. I like the solution, 2 questions if I may ask, how could I make it work for IE? it works for firefox ok but not for my IE browser. How could I use it if the two list values come from input textbox. Appreciate your help.
    Quote Originally Posted by jt107 View Post
    Thanks for your help. This one works perfectly. Appreciate your help!
    Just saw you questions...
    then assuming you are referring to post #3 ...
    AFAIK, the solution does work in MSIE. Might be your set-up, but I assume you have solved this portion because of the next post.

    Conversion of a string to an array is relatively simple:
    Code:
    // one possibility
    var list = 'a b c d e f'.split (' ');
    // or even
    var list = 'abcde'.split('');
    You should be able to acquire the text from the <input> element using .value syntax.

    Again, I assume you have solved this as well because of the last post.

    Therefore, you're most welcome, I'm sure, from both of use regardless of which solution you use.
    Happy to help.
    Good Luck!


    BTW: You should mark thread as resolved if you are satisfied with the solutions presented.
    Last edited by JMRKER; 06-28-2011 at 08:59 AM.

  7. #7
    Join Date
    Feb 2010
    Posts
    34

    resolved

    Thanks again. That is a very helpful explanation.

  8. #8
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,268
    What about this script ?
    HTML Code:
    	list1 = "a,b,c,d,e,f";
    	list2 ="d,f,w,x,y";
    	list=(list1+","+list2).split(/,/g).sort().join(',');
    alert(list)
    	while (list!=(listNoDubs=list.replace(/(,[^,]+)\1/,'$1'))) list=listNoDubs;
    alert(list)

  9. #9
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,268
    One line is enough !

    HTML Code:
    	list1 = "a,b,c,d,e,f";
    	list2 ="d,f,w,x,y";
    	list=(list1+","+list2).split(/,/g).sort().join(',').replace(/(,[^,]+)\1/g,'$1');
    Never pay webdeveloppers with lines !

  10. #10
    Join Date
    Feb 2010
    Posts
    34
    Quote Originally Posted by 007Julien View Post
    One line is enough !

    HTML Code:
    	list1 = "a,b,c,d,e,f";
    	list2 ="d,f,w,x,y";
    	list=(list1+","+list2).split(/,/g).sort().join(',').replace(/(,[^,]+)\1/g,'$1');
    Never pay webdeveloppers with lines !
    Come on, you make it so easy! hope people have some sense and not say, there are 10 thousand lines of code any more.

  11. #11
    Join Date
    Feb 2010
    Posts
    34
    Quote Originally Posted by 007Julien View Post
    One line is enough !

    HTML Code:
    	list1 = "a,b,c,d,e,f";
    	list2 ="d,f,w,x,y";
    	list=(list1+","+list2).split(/,/g).sort().join(',').replace(/(,[^,]+)\1/g,'$1');
    Never pay webdeveloppers with lines !
    Could you show me how could I make it work with space demiliter list?
    list1 = "a b c d e f";
    list2 ="d f w x y";
    list=(list1+" "+list2).split(/ /g).sort().join(' ').replace(/(s[^s]+)\1/g,'$1');
    alert(list);

    Thanks a lot.

  12. #12
    Join Date
    Feb 2010
    Posts
    34
    Ok find the problem, here is the corrent syntax:
    list=(list1+" "+list2).split(/ /g).sort().join(' ').replace(/(\s[^\s]+)\1/g,'$1');

    Thanks again!

  13. #13
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,268
    Bravo !

Thread Information

Users Browsing this Thread

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

Posting Permissions

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



Recent Articles