Write a Domain frequency ‘categorize’ function which accepts array of URLS and returns expected output

Input:

const urls = [
    'www.google.com',
    'www.yahoo.com',
    'xyz.www.google.com',
    'abc.xyz.www.google.com',
    'google.com',
    'yahoo.com',
    'news.yahoo.com',
    'abc.news.yahoo.com',
    'abc.xyz.www.google.com',
    'www.yahoo.com',
    'google.com',
];
categorize(urls);

The above categorize function should return below output.
For instance, ‘www.google.com’ is occuring in ‘www.google.com’, ‘xyz.www.google.com’, ‘abc.xyz.www.google.com’ etc.
‘google.com’ is occuring in ‘google.com’, ‘www.google.com’, ‘xyz.www.google.com’, ‘abc.xyz.www.google.com’ etc.

Expected Output:

{
 "google.com": 6,
 "yahoo.com": 5,
 "www.google.com": 4,
 "www.yahoo.com": 2,
 "news.yahoo.com": 2,
 "xyz.www.google.com": 3,
 "abc.news.yahoo.com": 1,
 "abc.xyz.www.google.com": 2,
 "com": 11
}

Source: Ask Javascript Questions

LEAVE A COMMENT