answersLogoWhite

0


Best Answer

As mentioned by others use String::split(), followed by some map (hashmap or linkedhashmap) and then merge your result. For completeness sake putting the code.

import java.util.*;

public class Genric<E>

{

public static void main(String[] args)

{

Map<String, Integer> unique = new LinkedHashMap<String, Integer>();

for (String string : "House, House, House, Dog, Dog, Dog, Dog".split(", ")) {

if(unique.get(string) == null)

unique.put(string, 1);

else

unique.put(string, unique.get(string) + 1);

}

String uniqueString = join(unique.keySet(), ", ");

List<Integer> value = new ArrayList<Integer>(unique.values());

System.out.println("Output = " + uniqueString);

System.out.println("Values = " + value);

}

public static String join(Collection<String> s, String delimiter) {

StringBuffer buffer = new StringBuffer();

Iterator<String> iter = s.iterator();

while (iter.hasNext()) {

buffer.append(iter.next());

if (iter.hasNext()) {

buffer.append(delimiter);

}

}

return buffer.toString();

}

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a Java code to find number of duplicate words in a sentence?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you write 90 in words?

The number 90 is written out in words as such: ninety. You should spell out a number rather than write it in number form in particular scenarios, such as if you are beginning a sentence with a number.


When do you write numbers as words?

In ordinary writing, the numbers one through ten are written as words. Higher numbers are written as numbers.A sentence should not start with a numeral, so write out the number (or reword the sentence).


How do you write athletic in a sentence?

words


Can you write a sentence using the word supplied?

Yes. You can write a sentence using any words that make sense.


How do you put wickedness in a sentence?

Write curse words in it!


What is the correct grammar for this sentence Write the word or words that goes in the sentence?

Write the word or words that goes go in the sentence. The subject is "word," singular, so the verb must agree by also being singular.


How do you write a sentence in an active voice?

To write a sentence in an active voice, the subject performs the action. In other words, it should be such that the subject of the sentence acts on the object.


How do you write a sentence with multiple words?

If you could write that question, then you already have the answer. It's a word followed by another. You can rarely write a sentence with only one word....


How do you do write a numeral?

You write the number in words &gt;:D


How do you write a long sentence?

by using a bunch or a lot of words


How do you write a sentence using the words All Together?

cloth


Can you write a sentence with the word dialog in it?

yes you can. did that sentence have the word 'dialogue" in it? you can combine many different words into a sentence.