eliminate dulplicates from array

10 views
Skip to first unread message

Alex Hough

unread,
Nov 25, 2009, 6:51:53 AM11/25/09
to TiddlyWiki
I've found a way to eliminate duplicates from an array [1] and applied it to my code. I am not familiar enough with functions to know why it does not work, but think that it is a simple solution.

the script is below

Alex

[1] http://stackoverflow.com/questions/840781/easiest-way-to-find-duplicate-values-in-a-javascript-array

<script>

function eliminateDuplicates(arr) {
  var i,
      len=arr.length,
      clean=[],
      obj={};

  for (i=0;i<len;i++) {
    obj[arr[i]]=0;
  }
  for (i in obj) {
    clean.push(i);
  }
  return clean;
}

var arrToClean=[];
var out=[];
tids=['Q1','Q2','Q3','Q4','Q5','Q6'];
for (var i=0; i<tids.length; i++) {
        var t=tids[i].title;
    var score=DataTiddler.getData(tids[i],"score");
    var val=store.getValue(tids[i],"primary");
    var val1=store.getValue(tids[i],"secondary");
    var filter="[tag["+val.readBracketedList().join("]][tag[")+"]]";
    var filter1="[tag["+val1.readBracketedList().join("]][tag[")+"]]";
    var list=store.filterTiddlers(filter).map(function(t){return"[["+t.title+"]]"});
    var list1=store.filterTiddlers(filter1).map(function(t){return"[["+t.title+"]]"});
       
arrToClean.push(list.concat(list1));

}
eliminateDuplicates (arrToClean);
out.push(clean);
return out.join("\n");
</script>


Mark S.

unread,
Nov 25, 2009, 3:46:51 PM11/25/09
to TiddlyWiki
Thought about using the hash compact method the other day ... can't
remember why I didn't pursue it.

Anyways, you're not using the object generated by the funtion. Try
changing this:

eliminateDuplicates(arrToClean);
out.push(clean);

to this:

out = eliminateDuplicates(arrToClean);

Mark

On Nov 25, 3:51 am, Alex Hough <r.a.ho...@googlemail.com> wrote:
> I've found a way to eliminate duplicates from an array [1] and applied it to
> my code. I am not familiar enough with functions to know why it does not
> work, but think that it is a simple solution.
>
> the script is below
>
> Alex
>
> [1]http://stackoverflow.com/questions/840781/easiest-way-to-find-duplica...

Alex Hough

unread,
Nov 25, 2009, 4:32:19 PM11/25/09
to tiddl...@googlegroups.com
Thanks Mark,

I'll try this. I'm going a few steps back to get a few steps forward here.

Could you provide a link the the 'hash compact method' ?

ALex

2009/11/25 Mark S. <thro...@yahoo.com>
--

You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To post to this group, send email to tiddl...@googlegroups.com.
To unsubscribe from this group, send email to tiddlywiki+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/tiddlywiki?hl=en.





--
http://www.multiurl.com/g/64

Mark S.

unread,
Nov 25, 2009, 5:38:12 PM11/25/09
to TiddlyWiki
'hash compact method' was just my terminology, referring to a
technique (like you're using) that appears in multiple books/
languages. In any language that supports a hash (and/or map) object,
you can eliminate multiples by mapping each tentative item to a
variable (typically a number). In some cases you might increment the
value each time you insert the item, thus automatically tracking the
number of occurrences of each item you are tracking. The hash
automatically "compacts" out all unique items, which you can then list
by iterating over the map or hash.

Mark

On Nov 25, 1:32 pm, Alex Hough <r.a.ho...@googlemail.com> wrote:
> Thanks Mark,
>
> I'll try this. I'm going a few steps back to get a few steps forward here.
>
> Could you provide a link the the 'hash compact method' ?
>
> ALex
>
> 2009/11/25 Mark S. <throa...@yahoo.com>
> > tiddlywiki+...@googlegroups.com<tiddlywiki%2Bunsu...@googlegroups.com>
> > .

Alex Hough

unread,
Nov 26, 2009, 3:48:06 AM11/26/09
to tiddl...@googlegroups.com
thanks for that Mark


"In some cases you might increment the value each time you insert the item, thus automatically tracking the number of occurrences of each item you are tracking"

this is a good idea: It could lead to the output being like a 'tag cloud'


Alex



 
To unsubscribe from this group, send email to tiddlywiki+...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/tiddlywiki?hl=en.





--
http://www.multiurl.com/g/64

Alex Hough

unread,
Nov 27, 2009, 11:02:36 AM11/27/09
to tiddl...@googlegroups.com
Hello there,

I'm struggling here [1]. I can't see why the function to remove duplicates is not working. The script is included below, the link is the function in a live TW.

I've spent ages looking a the this one and testing. I can't see the flaw in the logic.

ALex
[1] https://dl.dropbox.com/u/1316865/OMM/Omm/DirectorExerimental.html

arrUnique
========
<script>
//Adds new uniqueArr values to temp array (see- http://www.developersnippets.com/2008/10/30/remove-duplicates-from-array-using-javascript/)

function uniqueArr(a) {
    temp = new Array();
        for(i=0;i<a.length;i++){
            if(!contains(temp, a[i])){
            temp.length+=1;
                temp[temp.length-1]=a[i];
            }
        }
return temp;
}
 
//Will check for the Uniqueness
function contains(a, e) {
    for(j=0;j<a.length;j++)
    if(a[j]==e)return true;
return false;
}

var out=[],all=[];
tids=['Q1','Q2','Q3','Q4','Q5','Q6','Q7','Q8','Q9','Q10','Q11','Q12','Q13','Q14','Q15','Q16','Q17','Q18','Q19','Q20','Q21','Q22','Q23','Q24'];

for (var i=0; i<tids.length; i++) {
        var t=tids[i].title;
    var score=DataTiddler.getData(Questions[i],"score");

    var val=store.getValue(tids[i],"primary");
    var val1=store.getValue(tids[i],"secondary");
    var filter="[tag["+val.readBracketedList().join("]][tag[")+"]]";
    var filter1="[tag["+val1.readBracketedList().join("]][tag[")+"]]";
    var list=store.filterTiddlers(filter).map(function(t){return""+t.title+""});
    var list1=store.filterTiddlers(filter1).map(function(t){return""+t.title+""});
    var list2 =(list.concat(list1));
        if (score <= 2) {
        all.push(list2);
        }
}
out = uniqueArr(all);
return out.join(",");
</script>

Alex
[1]

2009/11/26 Alex Hough <r.a....@googlemail.com>



--
http://www.multiurl.com/g/64

Tobias Beer

unread,
Nov 27, 2009, 9:43:45 PM11/27/09
to TiddlyWiki
Hi Alex,


1) instead of:

temp.length+=1;
temp[temp.length-1]=a[i];


why not just...

temp.push(a[i]);

?


2) tiddlywiki has a native contains function used like this:

arr.contains(el);

so....

if(!contains(temp, a[i])){...

could just be...

if(!temp.contains(a[i])){...



3) why not use pushUnique to begin with and don't do any of that
uniqueArr() business to begin with...

all.pushUnique(list2);

?


4) However, if list2 actually is an array itself (haven't looked at it
that close)... you know that you'd be rather creating a subarray
within your 'all' array instead of adding individual items, right?


Tobias.
Reply all
Reply to author
Forward
0 new messages