logo Practice-It logo

countInAreaCode

Language/Type: Java Sets and Maps Collections
Author: Marty Stepp

Write a method countInAreaCode that accepts two parameters, a Map from names (strings) to phone numbers (strings) and an area code (as a string), and returns how many unique phone numbers in the map use that area code. For example, if a map m contains these pairs:

{Marty=206-685-2181, Rick=520-621-6126, Beekto=206-685-2181, 
Jenny=253-867-5309, Stuart=206-685-9138, DirecTV=800-494-4388,
Bob=206-685-9138, Benson=206-616-1246, Hottline=900-674-2767}

The call of countInAreaCode(m, "206") should return 3, because there are 3 unique phone numbers that use the 206 area code: Marty/Beekto's number of "206-685-2181", Stuart/Bob's number of "206-685-9138", and Benson's number of "206-616-1246".

You may assume that the map passed is not null, that no key or value in it is null, that every phone number value string in the map will begin with a 3-digit numeric area code, and that the area code string passed will be a non-null numeric string exactly 3 characters in length. If the map is empty or contains no phone numbers with the given area code, your method should return 0.

You may create one collection of your choice as auxiliary storage to solve this problem. You can have as many simple variables as you like. For full credit your code must run in less than O(n2) time where n is the number of pairs in the map.

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.