0

In a a dojo / javscript application

I am trying to figure the most efficient way to find the duplicates in this list of objects below:

{ item: "A", value: "one" }, 
{ item: "B", value: "two" },
{ item: "C", value: "three" },
{ item: "B", value: "two" },
{ item: "D", value: "five" },
{ item: "C", value: "three" }

so what I need as a finished product is:

not found list:

{ item: "A", value: "one" }, 
{ item: "D", value: "five" }

found list:

{ item: "B", value: "two" },
{ item: "C", value: "three" }

Also, I am using DOJO AMD so I know I have forEach, filter, some available to me

I know I can do a first pass to get the duplicate value:

var split = function( list )
{
  var found = [];
  var objectList = lang.clone( list );

  var test = objectList.pop();

    objectList.forEach( function( inner )
    {
        if( test.item == test.item )
        {
            found.push( test.item );
        }

    } );
return found;
};
robasc
  • 297
  • 1
  • 5
  • 15

2 Answers2

1

If you use Java: Stop using a list. Use a Set. This solves all your problems. Sry, you might be disappointed, but the answer is that simple.

PKlumpp
  • 3,965
  • 6
  • 31
  • 55
  • 2
    That depends. If OP needs to know which elements were duplicated a regular `Set` isn't the answer. – Keppil Jul 10 '14 at 13:41
  • Pretty sure their code is JavaScript. And even if it were Java, I don't think it is that simple. The objects inside the list (or set) will still be unique, even if its contents are the same. Wouldn't you have to override the `hashCode` (or whatever it is) to create an identifier? – Ian Jul 10 '14 at 13:43
  • I second @Keppil. He will need more than a `Set` to get what he wants. – TheLostMind Jul 10 '14 at 13:43
  • 1
    Guava has a `MultiSet` that might do the trick. But as @Ian points out, this is probably about Javascript anyway. – Keppil Jul 10 '14 at 13:44
0

Your best approach would be storing the data correctly.

It's possible that you still need to store non-unique items, if that's so - continue using an ArrayList, but in addition, use the following:

Override the hashcode & equals function as shown in this link: What issues should be considered when overriding equals and hashCode in Java?

Then, use a Set (HashSet would probably be enough for you) to store all your objects. This data structure will disregard elements which are not unique to elements already inside the set.

Then, all you need to do is query the size of the set, and that gives you the amount of unique elements in the list.

Community
  • 1
  • 1
Gil Moshayof
  • 16,053
  • 4
  • 41
  • 54
  • He tagged "java", so here's a java solution. – Gil Moshayof Jul 10 '14 at 13:45
  • Very true, but there's no mention of Java and the code is JavaScript, so why would you provide a Java solution? People use several similar tags (incorrect in this case), but it doesn't mean you should use that. If you're not sure what they're using, find out with comments – Ian Jul 10 '14 at 13:54
  • I assumed that since the "java" tag was present, that a java solution would be relevant, despite the example he posted was in javascript. If that is not the case, then the person who created this question wasted my time and yours, and you should be criticising him, not me. – Gil Moshayof Jul 10 '14 at 14:16