CSharp · Console
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
// I'm assuming zipcodes are ints... convert if desired
// jumbled up your sample data to show that the code would still work
var zipcodes = new List<int>{ 90012,
90033,
90009,
90001,
90005,
90004,
90041,
90008,
90007,
90031,
90010,
90002,
90003,
90034,
90032,
90006,
90022,};
// facilitate constant-time lookups of whether zipcodes are in your set
var zipHashSet = new HashSet<int>();
// lookup zipcode -> linked list node to remove item in constant time from the linked list
var nodeDictionary = new Dictionary<int, DoublyLinkedListNode<int>>();
// linked list for iterating and grouping your zip codes in linear time
var zipLinkedList = new DoublyLinkedList<int>();
// initialize our datastructures from the initial list
foreach (int zipcode in zipcodes) {
zipLinkedList.Add(zipcode);
zipHashSet.Add(zipcode);
nodeDictionary[zipcode] = zipLinkedList.Last;
}
// object to store the groupings (ex: "90001-90010", "90022")
var groupings = new HashSet<string>();
// iterate through the linked list, but skip nodes if we group it with a zip code
// that we found on a previous iteration of the loop
var node = zipLinkedList.First;
while (node != null) {
var bottomZipCode = node.Element;
var topZipCode = bottomZipCode;
// find the lowest zip code in this group
while (zipHashSet.Contains(bottomZipCode-1)) {
var nodeToDel = nodeDictionary[bottomZipCode-1];
// delete node from linked list so we don't observe any node more than once
if (nodeToDel.Previous != null) {
nodeToDel.Previous.Next = nodeToDel.Next;
}
if (nodeToDel.Next != null){
nodeToDel.Next.Previous = nodeToDel.Previous;
}
// see if previous zip code is in our group, too
bottomZipCode--;
}
// get string version zip code bottom of the range
var bottom = bottomZipCode.ToString();
// find the highest zip code in this group
while (zipHashSet.Contains(topZipCode+1)) {
var nodeToDel = nodeDictionary[topZipCode+1];
// delete node from linked list so we don't observe any node more than once
if (nodeToDel.Previous != null) {
nodeToDel.Previous.Next = nodeToDel.Next;
}
if (nodeToDel.Next != null){
nodeToDel.Next.Previous = nodeToDel.Previous;
}
// see if next zip code is in our group, too
topZipCode++;
}
// get string version zip code top of the range
var top = topZipCode.ToString();
// add grouping in correct format
if (top == bottom) {
groupings.Add(bottom);
} else {
groupings.Add(bottom + "-" + top);
}
// onward!
node = node.Next;
}
// print results
foreach(var grouping in groupings){
Console.WriteLine(grouping);
}
}
// you can find these classes in a gist at this link
// https://gist.github.com/frankbryce/919a4c4b8f3a447745e9
// it's almost copied from this link, except I added generics to it
// https://tdimov.wordpress.com/2013/03/10/doubly-linked-list-c-implementation/
public class DoublyLinkedListNode<T>
{
private T element;
private DoublyLinkedListNode<T> next;
private DoublyLinkedListNode<T> previous;
public DoublyLinkedListNode(T element)
{
this.element = element;
this.next = null;
this.previous = null;
}
public DoublyLinkedListNode(T element, DoublyLinkedListNode<T> prevNode)
{
this.element = element;
this.previous = prevNode;
prevNode.next = this;
}
public T Element
{
get { return this.element; }
set { this.element = value; }
}
public DoublyLinkedListNode<T> Next
{
get { return this.next; }
set { this.next = value; }
}
public DoublyLinkedListNode<T> Previous
{
get { return this.previous; }
set { this.previous = value; }
}
}
public class DoublyLinkedList<T>
{
private DoublyLinkedListNode<T> head;
private DoublyLinkedListNode<T> tail;
private int count;
public DoublyLinkedListNode<T> First { get { return head; } set { head = value; } }
public DoublyLinkedListNode<T> Last { get { return tail; } set { tail = value; } }
public DoublyLinkedList()
{
this.head = null;
this.tail = null;
this.count = 0;
}
public int Count
{
get { return this.count; }
}
public T this[int index]
{
get
{
if (index >= count || index < 0)
{
throw new ArgumentOutOfRangeException("Out of range!");
}
DoublyLinkedListNode<T> currentNode = this.head;
for (int i = 0; i < index; i++)
{
currentNode = currentNode.Next;
}
return currentNode.Element;
}
set
{
if (index >= count || index < 0)
{
throw new ArgumentOutOfRangeException("Out of range!");
}
DoublyLinkedListNode<T> currentNode = this.head;
for (int i = 0; i < index; i++)
{
currentNode = currentNode.Next;
}
currentNode.Element = value;
}
}
public void Add(T item)
{
if (this.head == null)
{
this.head = new DoublyLinkedListNode<T>(item);
this.tail = this.head;
}
else
{
DoublyLinkedListNode<T> newItem = new DoublyLinkedListNode<T>(item, tail);
this.tail = newItem;
}
count++;
}
public void Insert(T item, int index)
{
count++;
if (index >= count || index < 0)
{
throw new ArgumentOutOfRangeException("Out of range!");
}
var newItem = new DoublyLinkedListNode<T>(item);
int currentIndex = 0;
var currentItem = this.head;
DoublyLinkedListNode<T> prevItem = null;
while (currentIndex < index)
{
prevItem = currentItem;
currentItem = currentItem.Next;
currentIndex++;
}
if (index == 0)
{
newItem.Previous = this.head.Previous;
newItem.Next = this.head;
this.head.Previous = newItem;
this.head = newItem;
}
else if (index == count - 1)
{
newItem.Previous = this.tail;
this.tail.Next = newItem;
newItem = this.tail;
}
else
{
newItem.Next = prevItem.Next;
prevItem.Next = newItem;
newItem.Previous = currentItem.Previous;
currentItem.Previous = newItem;
}
}
public void Remove(object item)
{
int currentIndex = 0;
DoublyLinkedListNode<T> currentItem = this.head;
DoublyLinkedListNode<T> prevItem = null;
while (currentItem != null)
{
if ((currentItem.Element != null &&
currentItem.Element.Equals(item)) ||
(currentItem.Element == null) && (item == null))
{
break;
}
prevItem = currentItem;
currentItem = currentItem.Next;
currentIndex++;
}
if (currentItem != null)
{
count--;
if (count == 0)
{
this.head = null;
}
else if (prevItem == null)
{
this.head = currentItem.Next;
this.head.Previous = null;
}
else if (currentItem == tail)
{
currentItem.Previous.Next = null;
this.tail = currentItem.Previous;
}
else
{
currentItem.Previous.Next = currentItem.Next;
currentItem.Next.Previous = currentItem.Previous;
}
}
}
public void RemoveAt(int index)
{
if (index >= this.count || index < 0)
{
throw new ArgumentOutOfRangeException("Out of range!");
}
int currentIndex = 0;
DoublyLinkedListNode<T> currentItem = this.head;
DoublyLinkedListNode<T> prevItem = null;
while (currentIndex < index)
{
prevItem = currentItem;
currentItem = currentItem.Next;
currentIndex++;
}
if (this.count == 0)
{
this.head = null;
}
else if (prevItem == null)
{
this.head = currentItem.Next;
this.head.Previous = null;
}
else if (index == count - 1)
{
prevItem.Next = currentItem.Next;
tail = prevItem;
currentItem = null;
}
else
{
prevItem.Next = currentItem.Next;
currentItem.Next.Previous = prevItem;
}
count--;
}
public int indexOf(T item)
{
int index = 0;
DoublyLinkedListNode<T> currentItem = this.head;
while (currentItem != null)
{
if (((currentItem.Element != null) && (item.Equals(currentItem.Element))) ||
((currentItem.Element == null) && (item == null)))
{
return index;
}
index++;
currentItem = currentItem.Next;
}
return -1;
}
public bool Contains(T element)
{
int index = indexOf(element);
bool contains = (index != -1);
return contains;
}
public void Clear()
{
this.head = null;
this.tail = null;
this.count = 0;
}
}
}