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?
<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.
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.
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...
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;
}
Bookmarks