Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

wordcount challenge

Hi,

I am working on below challenge.
http://codingbat.com/prob/p117630

The classic word-count algorithm: given an array of strings, return a Map<String, Integer> with a key for each different string, with the value the number of times that string appears in the array.

wordCount(["a", "b", "a", "c", "b"]) → {"b": 2, "c": 1, "a": 2}
wordCount(["c", "b", "a"]) → {"b": 1, "c": 1, "a": 1}
wordCount(["c", "c", "c", "c"]) → {"c": 4}

do i need to worry about sorting like alphabetica in output?
ASKER CERTIFIED SOLUTION
Avatar of Am P
Am P
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

public Map<String, Integer> wordCount(String[] strings) {
  Map<String, Integer> map=new HashMap();
  for(int i=0;i<strings.length;i++){
  String test=strings[i];
  if(map.containsKey(test)){
  int count=map.get(test);
  map.put(test,count+1);
  }
  
  else{
  map.put(test,1);
  }
}
  return map;
}

Open in new window


above passes all tests. any improvements or alternate approaches.
Avatar of gudii9

ASKER

public Map < String, Integer > wordCount(String[] strings) {
    Map < String, Integer > map = new SortedMap();
    for (int i = 0; i < strings.length; i++) {
        String test = strings[i];
        if (map.containsKey(test)) {
            int count = map.get(test);
            map.put(test, count + 1);
        } else {
            map.put(test, 1);
        }
    }
    return map;
}

Open in new window


why above gives below compilation error?
Compile problems:


Error:      Map<String, Integer> map=new SortedMap();
                                   ^^^^^^^^^
Cannot instantiate the type SortedMap


see Example Code to help with compile problems
does not codingbat supports SortedMap?
Avatar of phoffric
phoffric

Working with SortedMap in this question is off-topic as the challenge questions requires you to use a Map.
You should look up the SortedMap API if you want to ask another question.
Avatar of gudii9

ASKER

SortedMap is implementation of Map interface similar to HashMap right? I thought codingbat supports that as well but looks no. Only HashMap? I will test in ecipse and see how it works
Avatar of gudii9

ASKER

looks like no SortedMap class is there. It seems to be interface. TreeMap is only implenetation class.
https://www.tutorialspoint.com/java/java_sortedmap_interface.htm
Avatar of gudii9

ASKER

package test;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class WordCount {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// wordCount(["a", "b", "a", "c", "b"]) → {"b": 2, "c": 1, "a": 2}
		// Map<String, String> map = new HashMap();
		// map.put("b", null);
		// map.put("a", "Hi");
		String[] arr = { "a", "b", "a", "c", "b" };
		System.out.println("--->" + mapAB(arr));

	}

	/*
	 * private static String mapAB(String[] arr) { // TODO Auto-generated method
	 * stub return null; }
	 */

	public Map<String, Integer> wordCount(String[] strings) {
		Map<String, Integer> map = new TreeMap();
		for (int i = 0; i < strings.length; i++) {
			String test = strings[i];
			if (map.containsKey(test)) {
				int count = map.get(test);
				map.put(test, count + 1);
			}

			else {
				map.put(test, 1);
			}
		}
		return map;
	}

}

Open in new window


above gives below error at line at line 9

The method mapAB(String[]) is undefined for the type WordCount

Not sure why and how to fix it?
Avatar of gudii9

ASKER

package test;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class WordCount {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// wordCount(["a", "b", "a", "c", "b"]) → {"b": 2, "c": 1, "a": 2}
		// Map<String, String> map = new HashMap();
		// map.put("b", null);
		// map.put("a", "Hi");
		String[] arr = { "a", "b", "a", "c", "b" };
		System.out.println("--->" + wordCount(arr));

	}

	/*
	 * private static String mapAB(String[] arr) { // TODO Auto-generated method
	 * stub return null; }
	 */

	public static Map<String, Integer> wordCount(String[] strings) {
		Map<String, Integer> map = new TreeMap();
		for (int i = 0; i < strings.length; i++) {
			String test = strings[i];
			if (map.containsKey(test)) {
				int count = map.get(test);
				map.put(test, count + 1);
			}

			else {
				map.put(test, 1);
			}
		}
		return map;
	}

}

Open in new window


i fixed it. now i see sorted key output

--->{a=2, b=2, c=1}
Avatar of gudii9

ASKER

package test;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class WordCount {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// wordCount(["a", "b", "a", "c", "b"]) → {"b": 2, "c": 1, "a": 2}
		// Map<String, String> map = new HashMap();
		// map.put("b", null);
		// map.put("a", "Hi");
		String[] arr = { "a","e","b", "a", "c", "b" };
		System.out.println("--->" + wordCount(arr));

	}

	/*
	 * private static String mapAB(String[] arr) { // TODO Auto-generated method
	 * stub return null; }
	 */

	public static Map<String, Integer> wordCount(String[] strings) {
		Map<String, Integer> map = new HashMap();
		for (int i = 0; i < strings.length; i++) {
			String test = strings[i];
			if (map.containsKey(test)) {
				int count = map.get(test);
				map.put(test, count + 1);
			}

			else {
				map.put(test, 1);
			}
		}
		return map;
	}

}

Open in new window


even after changing TreeMap to HashMap back again why and how it is sorting?

--->{a=2, b=2, c=1, e=1}
Avatar of gudii9

ASKER

public Map<String, Integer> wordCount(String[] strings) {
  Map<String, Integer> map=new TreeMap();
  for(int i=0;i<strings.length;i++){
  String test=strings[i];
  if(map.containsKey(test)){
  int count=map.get(test);
  map.put(test,count+1);
  }
  
  else{
  map.put(test,1);
  }
}
  return map;
}

Open in new window


codingbat supported TreeMap as well