1

Is there a technique or an easy way in returning all the duplicate values, and also if possible, their indices. And also the unique ones, and their indices. How can I achieve this? (may or may not be using objects)

Ex.

var setOfValues = [1, 2, 2, 2, 3, 3, 4, 4, 4, 5, 10, 10, 9, 10, 8 , 4, 11];

unique = {
    values: [1, 5, 9, 8, 4, 11],
    indices: [0, 9, 12, 14, 15, 16]
};

duplicates = {
    values: [2, 2, 2, 3, 3, 4, 4, 4, 10, 10, 10],
    indices: [1, 2, 3, 4, 5, 6, 7, 8, 10, 11]
};
8
  • Have you tried anything? Commented Mar 8, 2014 at 17:39
  • yes, I already tried to find it out, but it seems it requires, deep algorithm O.O, please help! Commented Mar 8, 2014 at 17:40
  • Can you post what you have tried? Commented Mar 8, 2014 at 17:41
  • stackoverflow.com/questions/5381621/… or stackoverflow.com/questions/11246758/… Commented Mar 8, 2014 at 17:44
  • posting the code Sir here is irrelevant since the values are not exactly or close to it. Just want to know if there's an easy way getting what I want. Appreciate your reply :) Commented Mar 8, 2014 at 17:48

2 Answers 2

0

I would rather take this as an algorithm question instead of javascript question

You can try to sort the array first then it's easy to come up with an O(n) algorithm to find out all duplicates and number of copies on each.

Then you can search the original array with duplicates you found to determine the original indexes of duplicated items.

Sign up to request clarification or add additional context in comments.

2 Comments

oh! Sorry about that Sir, I'm just new here, is there a way to change the category or title here?
you can edit your original post then scroll down to bottom to add another tag, just to attract more people to provide you an answer, however your question isn't big one so i think those comment shown in your main post has some pretty good links to other questions there. or you can try to implement my suggestion yourself.
0

I believe this does what you are asking, though the output is different to your example expectation, so maybe it's not but I can't compare against what you tried, can you clarify?

Javascript

function getUniqueAndduplicatesWithIndicies(array) {
    return array.reduce(function (acc, curr, index, arr) {
        if (acc.duplicates.values.indexOf(curr) !== -1 || arr.lastIndexOf(curr) !== index) {
            acc.duplicates.values.push(curr);
            acc.duplicates.indicies.push(index);
        } else {
            acc.unique.values.push(curr);
            acc.unique.indicies.push(index);
        }

        return acc;
    }, {
        unique: {
            values: [],
            indicies: []
        },
        duplicates: {
            values: [],
            indicies: []
        }
    });
}

var setOfValues = [1, 2, 2, 2, 3, 3, 4, 4, 4, 5, 10, 10, 9, 10, 8, 4, 11];

console.log(JSON.stringify(getUniqueAndduplicatesWithIndicies(setOfValues)));

Output

{
    "unique":{
        "values":[1,5,9,8,11],
        "indicies":[0,9,12,14,16]
    },
    "duplicates":{
        "values":[2,2,2,3,3,4,4,4,10,10,10,4],
        "indicies":[1,2,3,4,5,6,7,8,10,11,13,15]
    }
} 

On jsFiddle

4 Comments

Thank you Sir! It's the same Sir! I just forgot number 10 and 4 at the end. Thank you! Guess there's really a genius out there in terms of algorithms!
If maybe somehow there's an explanation how the code works that way, I can learn also and improve! Can I be your apprentice? :D
hmmmm, what If that each value in the setOfvalues, are from an Obj? or let me rephrase it. What if the arguments passed on the getUniqueAndduplicatesWithIndicies() is an object?
If the values in the array can be compared for equality, then it works (NaN can not be compared like this because NaN !== NaN). The code is very simple, what do you not understand? Step through all values while keeping an accumulator. If the current value matches a value already in duplicates or if the current value has another value with a different index in the array being tested, then it is a duplicate and so store value and index in duplicates. Otherwise it is unique so store the value and index in unique.

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.