Solved

wordcount challenge

Posted on 2016-09-30
11
123 Views
Last Modified: 2016-10-04
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?
0
Comment
Question by:gudii9
11 Comments
 
LVL 21

Accepted Solution

by:
Amitkumar Panchal earned 250 total points
ID: 41824748
Similar question as mentioned https://www.experts-exchange.com/questions/28973619/wordmultiple-challenge.html?cid=1752.

Answer is the same. :) Example results are not ordered, so need not to worry about.
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 250 total points
ID: 41826278
>> do i need to worry about sorting like alphabetica in output?
The challenge doesn't mention that, so: No.
Moreover, the're talking about returning a Map.
A Map hasn't any sorting of its keys.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41826807
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.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 7

Author Comment

by:gudii9
ID: 41826810
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?
0
 
LVL 32

Expert Comment

by:phoffric
ID: 41826916
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.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41826991
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
0
 
LVL 7

Author Comment

by:gudii9
ID: 41828698
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
0
 
LVL 7

Author Comment

by:gudii9
ID: 41828707
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?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41828719
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}
0
 
LVL 7

Author Comment

by:gudii9
ID: 41828722
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}
0
 
LVL 7

Author Comment

by:gudii9
ID: 41828740
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
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
even odd program using while loop 3 44
Why my table column Id is not passed to java object? 4 40
Problem to App source 6 42
Problem to Alipay 10 24
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question