I recently had to write the following algorithm:
Given a group of tags, and a group of
blog posts, where a blog post may
contain zero-to-many tags, return the
tags common to all posts.
This comparison is being done in-memory - accessing either collection does not cause a trip across the network (ie., to a database, etc).
Tags collection does not have a reference to
BlogPosts that contain it.
BlogPosts have a collection of
Tags they contain.
Below is my implementation. It performs just fine, but I'm curious if there was a better way to implement it.
My implementation is in Actionscript, but I'm curious more from a algorithim perspective, so examples in any language is fine. (But if I don't know the language, I may ask you to clarify some aspects)
Any examples of improvements would be greatly received.
private function getCommonTags(blogPosts:Vector.<BlogPost>):Vector.<Tag>
var commonTags:Vector.<Tag> = new Vector.<Tag>();
if (!blogPosts || blogPosts.length == 0)
var blogPost:BlogPost = blogPosts;
if (!blogPost.tags || blogPost.tags.length == 0)
commonTags = Vector.<Tag>(blogPosts.tags);
for each (var blogPost:BlogPost in blogPosts)
if (!blogPost.tags || blogPost.tags.length == 0 || commonTags.length == 0)
// Updated to fix bug mentioned below
// Optomized exit - there are no common tags
return new Vector.<Tag>();
for each (var tag:Tag in commonTags)