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]
};
Simon Halsey
  • 5,459
  • 1
  • 21
  • 32
meetmahpuppy
  • 418
  • 1
  • 4
  • 17
  • Have you tried anything? – Xotic750 Mar 08 '14 at 17:39
  • yes, I already tried to find it out, but it seems it requires, deep algorithm O.O, please help! – meetmahpuppy Mar 08 '14 at 17:40
  • Can you post what you have tried? – Xotic750 Mar 08 '14 at 17:41
  • http://stackoverflow.com/questions/5381621/jquery-function-to-get-all-unique-elements-from-an-array or http://stackoverflow.com/questions/11246758/how-to-get-unique-values-in-a-array – NickOpris Mar 08 '14 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 :) – meetmahpuppy Mar 08 '14 at 17:48
  • There is no native single function to achieve your desired result. So, a function would need to be created to search through the array for duplicates, record them and their indices. Then any index that is not a duplicate record that along with the unique value. – Xotic750 Mar 08 '14 at 17:52
  • @nickOpris thank you sir for the link, the unique seems pretty great! but all I really really want are those duplicated values to be returned. – meetmahpuppy Mar 08 '14 at 17:52
  • Your example data doesn't seem to match your description. Which one is correct? – Xotic750 Mar 08 '14 at 19:12

2 Answers2

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.

miushock
  • 1,087
  • 7
  • 19
  • oh! Sorry about that Sir, I'm just new here, is there a way to change the category or title here? – meetmahpuppy Mar 08 '14 at 17:50
  • 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. – miushock Mar 08 '14 at 17:51
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

Xotic750
  • 22,914
  • 8
  • 57
  • 79
  • 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! – meetmahpuppy Mar 09 '14 at 04:37
  • 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 – meetmahpuppy Mar 09 '14 at 04:40
  • 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? – meetmahpuppy Mar 09 '14 at 06:12
  • 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. – Xotic750 Mar 09 '14 at 10:04