0

I have a javascript associative array. It will be populated dynamically.

I want to find the keys which will contain the duplicate data/values.

For simplicity, it will look like this.

var p =   { };
 p =
    {
        "p1": "value1",
        "p2": "value2",
        "p3": "value3",
        "p4": "value2",
        "p5": "value3",
        "p6": "value5"
    };

How can i do this.

Here's the js fiddle.

jsfiddle

Is there other way than using hasOwnProperty?

Any ideas to make this work?

7
  • 2
    You cannot have two p5 keys! The second one will override the first one. It's like it was never even there. Commented Sep 19, 2013 at 22:51
  • P.S. This isn't an "associative array", it's an "object". Commented Sep 19, 2013 at 22:52
  • Sorry fixed the duplicate key issue. Commented Sep 19, 2013 at 22:53
  • 2
    hasOwnProperty won't help you find duplicate values. That checks to see if an object has a certain "key". Commented Sep 19, 2013 at 22:54
  • What was your issue or what is your issue? Commented Sep 19, 2013 at 23:00

3 Answers 3

3
var p =
    {
        "p1": "value1",
        "p2": "value2",
        "p3": "value3",
        "p4": "value2",
        "p5": "value3",
        "p6": "value5"
    };

var q = {};
var keys = Object.keys(p);
for (var i = 0; i < keys.length; i++) {
    var currentKey = keys[i];
    var newKey = p[currentKey];
    if (q[newKey] === undefined){
        q[newKey] = [];
    }
    q[newKey].push(currentKey);
}

This will get you an object looking something like:

{
  "value1": ["p1"],
  "value2": ["p2","p4"],
  "value3": ["p3","p5"],
  "value5": ["p6"]
}
Sign up to request clarification or add additional context in comments.

Comments

2

A slightly tighter version of @RocketHazmat's solution:

var values = {},
    dupes = [],
    key,
    val;
for (key in p) {
    if (p.hasOwnProperty(key)) {
        val = p[key];
        if (values[val]) dupes.push(key);
        else values[val] = true;
    }
}

This just gives you a list of keys with dupes. (Note - the first instance of a dupe value isn't considered a dupe here.)

2 Comments

Thanks alot for your answer, really appreciate your help. I added the fiddle here. jsfiddle.net/xVvj2
You could also put the assignment inside the brackets: if (values[val = p[key]]), even shorter.
2

Simplest way I can think of is to loop through your current object, and mark each value you see. When you see a value again, mark that key.

var tmp = {};
for(var i in p){
    var val = p[i];

    // Have we seen this value before?
    if(!tmp.hasOwnProperty(val)){
        // Mark the value as seen for the first time
        tmp[val] = []; // 0 duplicates
    }
    else{
        // We've seen it before, save the duplicate key
        tmp[val].push(i);
    }
}

// Print out values and their duplicate keys
for(var i in tmp){
    var keys = tmp[i];

    // Are there any duplicates?
    if(keys.length){
        alert(i + ' has multiple keys: '+keys.join(', '));
    }
}

DEMO: http://jsfiddle.net/bjRDK/

3 Comments

I am torn whether to mark your answer as accepted one or nrabinowitz. Yours uses two loops while nrabinowitz uses one. But you answered first. All i want in the end is the keys, but if i remove the i + ' has multiple keys: ' part in the end i get the keys etc . His solution also gives dupes as well etc. Try to be fair here,
@Nomad: Pick whichever one you like better / works better for you. It shouldn't matter who posted first. The point of accepting an answer is to pick which you think is "best", shouldn't matter who posted and when :-)
I did. Thank you so much for your time and help, I really really appreciate it. I liked the other solution better, hope you wont mind. Once again thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.