0

I have an array with domain names without www in JavaScript. This array may contain duplicate records. I want to find all unique domains and their occurrence counts. Plz Help.

Here is the current code

exports.findUniqueDomains = function(uri_arr){
    uri_arr.forEach(function(elem, index, arr){
        arr[index] = elem.split('.')
            .slice(-2)
            .join('.');
    });

    return uri_arr.filter (function (v, i, a) { return a.indexOf (v) == i });
};
a4arpan
  • 1,828
  • 2
  • 24
  • 41
  • What do you need help with? Just loop over the array and find what you need. – Razvan Jun 03 '14 at 08:04
  • Did you already try something? What was the problem with your approach? – t.niese Jun 03 '14 at 08:04
  • Can I do something with my existing function to get the result? – a4arpan Jun 03 '14 at 08:36
  • The first part of your code removes the `www.` from your domain. So replacing the second part with the answer of the linked question would allow you create two arrays one without duplicates and one with their occurrence. So what is missing to solve your problem? – t.niese Jun 03 '14 at 08:49

1 Answers1

0
  1. Make an object for keeping the values
  2. Iterate through the array, each time inserting new entry / incrementing counter in the counting object

For iteration, you can use for...in, .forEach or some other method.

MightyPork
  • 18,270
  • 10
  • 79
  • 133