www.webdeveloper.com
Results 1 to 11 of 11

Thread: reving duplicate text

  1. #1
    Join Date
    May 2010
    Posts
    5

    reving duplicate text

    So basically im trying to create a function that checks lines entered into a text box that removes duplicates completely.

    For example in the text box this is wrote.

    The
    And
    To
    What
    Where
    The
    One
    And
    Where

    I dont want duplicates changing to one. I want them out. So it reads


    To
    What
    One

    The order in what is left doesn't matter. Whether the results are on the page or in the text box doesn't matter. However, the user enters the text, it wont be those words.

    so far i have this

    Code:
    function noDupes (arr) {
         var tmp = new Array(), results = new Array();
         for (var m = arr.length - 1; m > -1; m--) {
              if (tmp[arr[m]] === undefined) tmp[arr[m]] = 1;
              else tmp[arr[m]] = undefined;
         }
         
         for (var t in tmp) {
              if (t !== undefined) results.push(t);
         }
         return results;
    }
    Unless this isn't the beat way to do it

    After naming the text box in the html how do I make the javascript read what is in there. And hoe do I make each line in the text box add to the array?

    Please?

  2. #2
    Join Date
    May 2010
    Posts
    5
    Sorry, spelling mistake in title. REMOVING duplicate text

  3. #3
    Join Date
    Aug 2007
    Posts
    3,767
    If it has an id, this is how.
    Code:
    var txtbox = document.getElementById("theid");
    txtbox.value = noDupes(txtbox.value.split(/\n/)).join("\n");
    split(/\n/) changes a string into an array, splitting on every new line, join "\n" does the opposite.
    Great wit and madness are near allied, and fine a line their bounds divide.

  4. #4
    Join Date
    May 2010
    Posts
    5
    Thanks Dec.

    I understand what you mean more than the books I've been stuck in.

    Could you possible tell me what I've done wrong here.

    Code:
    <html>
    <head>
        <title>Reomove Dupes</title></head>
            <body>
               
    <form>
    <textarea rows="30" cols="100" id="list"> </textarea>
     <br>
     <input type="submit" value="Submit" />
     
     </form>
    
    <script>
    
    function noDupes (arr) {
         var tmp = new Array(), results = new Array();
         for (var m = arr.length - 1; m > -1; m--) {
              if (tmp[arr[m]] === undefined) tmp[arr[m]] = 1;
              else tmp[arr[m]] = undefined;
         }
         
         for (var t in tmp) {
              if (t !== undefined) results.push(t);
         }
         return results;
    }
    
    var txtbox = document.getElementById("list");
    txtbox.value = noDupes(txtbox.value.split(/\n/)).join("\n");
    
                </script>
            </body>
    </html>

  5. #5
    Join Date
    Aug 2007
    Posts
    3,767
    Well, first to fix up your HTML a bit:
    Code:
    <html>
    <head>
        <title>Reomove Dupes</title></head>
            <body>
               
    <form onsubmit="removeDups(); return false;">
    <textarea rows="30" cols="100" id="list"> </textarea>
    <br>
    <input type="submit" value="Submit" />
    </form>
    <script type="text/javascript">
    function noDupes (arr) {
         var tmp = new Array(), results = new Array();
         for (var m = arr.length - 1; m > -1; m--) {
              if (tmp[arr[m]] === undefined) tmp[arr[m]] = 1;
              else tmp[arr[m]] = undefined;
         }
         
         for (var t in tmp) {
              if (t !== undefined) results.push(t);
         }
         return results;
    }
    function removeDups() {
    var txtbox = document.getElementById("list");
    txtbox.value = noDupes(txtbox.value.split(/\n/)).join("\n");
    }
    </script>
    </body>
    </html>
    The problem was the JavaScript was running immediately on page load, not where there was anything in the textarea. And I'm presuming your noDupes function works, I didn't check it.
    Great wit and madness are near allied, and fine a line their bounds divide.

  6. #6
    Join Date
    May 2010
    Posts
    5
    thanks

  7. #7
    Join Date
    Apr 2010
    Location
    UK
    Posts
    117
    Doesn't completely remove the duplicates for me, just leaves one copy behind - I thought you wanted them out?

    You could also make the code a little simpler if you work on a sorted array:
    Code:
    function noDupes (arr) {
        results = [];
        for (var i = 0, word; word = arr[i]; i++) {
            if (word !== arr[i + 1]) {
                results.push(word);
            }
        }
        return results;
    }
    function removeDups() {
        var txtbox = document.getElementById("list");
        txtbox.value = noDupes(txtbox.value.split(/\n/).sort()).join("\n");
    }
    Last edited by NicTlt; 05-07-2010 at 03:35 PM. Reason: Added alternative.
    Here begynith a techynge of the newe Scrypte with many other helpy thynges, etc

  8. #8
    Join Date
    May 2010
    Posts
    5
    Nic you're right

    I wanted them out completely. However I didn't want to be rude and I thought I could solve that myself. I can't.

    Any idea how to completely remove anything that's duplicated?

  9. #9
    Join Date
    Aug 2007
    Posts
    3,767
    As I said, I didn't check your function, you should have mentioned it!
    Code:
    function noDupes (arr) {
         var tmp = {}, results = [];
         for (var m = arr.length - 1; m > -1; m--) {
              if (tmp[arr[m]]===undefined) tmp[arr[m]] = true;
              else {
                   tmp[arr[m]] = false;
              }
         }
         
         for (var t in tmp) {
              if (tmp[t]) results.push(t);
         }
         return results;
    }
    That works as you want it to, or you could alter the sorted array version, but I don't think it's that elegant.
    Great wit and madness are near allied, and fine a line their bounds divide.

  10. #10
    Join Date
    Apr 2010
    Location
    UK
    Posts
    117
    Alternatively, with the sorted version, you can use splice() on the original array, since the duplicates will be adjacent:
    Code:
    function noDupes (arr) {
        for (var i = 0, word; word = arr[i]; i++) {
            var dupes = 1;
            while (word === arr[i + dupes]) {
                dupes++;
            }
            if (dupes > 1) {
                arr.splice(i, dupes);
            }
        }
        return arr;
    }
    And you can also do this, if you're a fan of chaining:
    Code:
    Array.prototype.noDupes = function () {
        for (var i = 0, word; word = this[i]; i++) {
            var dupes = 1;
            while (word === this[i + dupes]) {
                dupes++;
            }
            if (dupes > 1) {
                this.splice(i, dupes);
            }
        }
        return this;
    }
    
    //and add another link to the chain:
    function removeDups() {
        var txtbox = document.getElementById("list");
        txtbox.value = txtbox.value.split(/\n/).sort().noDupes().join("\n");
    }
    but I do tend to stray into the realms of unreadability...
    Here begynith a techynge of the newe Scrypte with many other helpy thynges, etc

  11. #11
    Join Date
    Aug 2009
    Posts
    593
    My version if anyone is interested... :P

    Code:
    function noDupes(arr)
    {
    	var i, j, results = [];
    	for (i = 0; i < arr.length; i++) {
    		for (j = 0; j < arr.length; j++) {
    			if (i !== j && arr[i] === arr[j]) {break;}
    		}
    		if (j >= arr.length) {results.push(arr[i]);}
    	}
    	return results;
    }

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