Solved

wordmultiple challenge

Posted on 2016-09-30
12
119 Views
Last Modified: 2016-10-05
Hi,

I am working on below challenge




wordMultiple
Given an array of strings, return a Map<String, Boolean> where each different string is a key and its value is true if that string appears 2 or more times in the array.

wordMultiple(["a", "b", "a", "c", "b"]) → {"b": true, "c": false, "a": true}
wordMultiple(["c", "b", "a"]) → {"b": false, "c": false, "a": false}
wordMultiple(["c", "c", "c", "c"]) → {"c": true}

do i need to care about the output sorting of values like alphabetical ascending or descending etc.

please advise
0
Comment
Question by:gudii9
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 21

Expert Comment

by:Amitkumar Panchal
ID: 41824741
Looking at results, sorting is not the requirement.
{"b": true, "c": false, "a": true}
{"b": false, "c": false, "a": false}
{"c": true}

You will need to iterate thru an array and keep a count of strings. Once counted then you need to check if it is more than two.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 41826348
do i need to care about the output sorting of values like alphabetical ascending or descending etc.
Does the challenge asks for that? No.
It asks for return a Map. And by nature a Map simply has no key sorting. (a SortedMap has)
0
 
LVL 7

Author Comment

by:gudii9
ID: 41826614
Here is actual link

http://codingbat.com/prob/p190862

It asks for return a Map. And by nature a Map simply has no key sorting. (a SortedMap has)
only Map not SorterMap here.
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 250 total points
ID: 41826771
If you click on the MAP link in the challenge question, you see that you are actually returning a hashmap which is not sorted.
As you know, hashing usually removes any sorted order.
So the answer to your question is: No.

Even the examples you show in the OP show no ordering.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41826926
public Map < String, Boolean > wordMultiple(String[] strings) {
    Map < String, Boolean > boolMap = new HashMap();
    Map < String, Integer > intMap = new HashMap();

    for (int i = 0; i < strings.length; i++) {
        String buffer = strings[i];

        if (intMap.containsKey(buffer)) {
            int val = intMap.get(buffer);
            val++;
            intMap.put(buffer, val);
        } else {
            intMap.put(buffer, 1);
        }
        boolMap.put(buffer, intMap.get(buffer) >= 2);

    }

    return boolMap;
}

Open in new window


above passes all. any improvements or alternate aproaches?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 32

Expert Comment

by:phoffric
ID: 41827402
Here's a tiny improvement, IMO:
    for (String buffer:strings) {
//        String buffer = strings[i];

Open in new window

Nice indenting, BTW.
0
 
LVL 37

Accepted Solution

by:
zzynx earned 250 total points
ID: 41827513
If you would like to use

 for (String buffer:strings) {

Open in new window


that's a replacement for these TWO lines of code:

for (int i = 0; i < strings.length; i++) {
        String buffer = strings[i];

Open in new window


Another imrpovement:
1) no need for intermediate variables
2) once you've counted two instances, there's no need to further "manipulating" your maps. Since it won't change the outcome.

public Map<String, Boolean> wordMultiple(String[] strings) {
   Map < String, Boolean > boolMap = new HashMap();
   Map < String, Integer > intMap = new HashMap();
  
   for (String buffer:strings) {
      if (boolMap.containsKey(buffer) && boolMap.get(buffer)) {
        continue;
      }
      if (intMap.containsKey(buffer)) {
          intMap.put(buffer, intMap.get(buffer)+1);
      } else {
          intMap.put(buffer, 1);
      }
      boolMap.put(buffer, intMap.get(buffer) >= 2);
   }
   return boolMap;    
}

Open in new window

0
 
LVL 32

Expert Comment

by:phoffric
ID: 41827531
@zzynx,
In the first half of  your last post #a41827513 you say, "that's a replacement for these TWO lines of code:"

But I demonstrated that in the post before you. Why did you repeat this small point?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 41829420
@phoffric
You told that
for (String buffer:strings) {

Open in new window

is a replacement for
//        String buffer = strings[i];

Open in new window

Which is in correct.

I corrected that. That's why I wrote TWO with capitals.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 41829962
Ok. No replacement since a for loop can hardly replace a definition of a variable. Just replacing one for loop with another and then commenting out the duplicate definition of buffer.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 41830023
Just replacing one for loop with another
That wasn't clear to me.

It's always good to use the form
replace
this code

Open in new window

by
that code

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now