program to determine number of duplicates in a sentence
<p>Code:public class duplicate</p>

<p>{</p>

<pre><code>public static void main(String[] args)throws IOException

  {

  System.out.println("Enter words separated by spaces ('.' to quit):");

  Set&lt;String&gt; s = new HashSet&lt;String&gt;();

  Scanner input = new Scanner(System.in);

  while (true)

  {

  String token = input.next();

  if (".".equals(token))

  break;

  if (!s.add(token))

  System.out.println("Duplicate detected: " + token);

  }

  System.out.println(s.size() + " distinct words:\n" + s);
 Set&lt;String&gt; duplicatesnum = new HashSet&lt;String&gt;();
</code></pre>

<p>String token = input.next();
if (!s.add(token))
{
    duplicatesnum.add(token);
    System.out.println("Duplicate detected: " + token);
}</p>

<p>System.out.println(duplicatesnum.size());</p>

<p>}
}
the output is:
Enter words separated by spaces ('.' to quit):
one two one two .
Duplicate detected: one
Duplicate detected: two
2 distinct words:
[two, one]</p>
