Find the Words in the Argument List that Occur Once, that Occur More than Once

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - Find the Words in the Argument List that Occur Once, that Occur More than Once

This Java example program shows how to find the words in the argument list than occur once, or that occur more than once.

[edit] Java Source Code

  • Package: com.sun.example
  • File: FindDupsAndUnique.java
package com.sun.example;
 
import java.util.HashSet;
import java.util.Set;
 
public class FindDupsAndUnique {
    public static void main(String args[]) {
	Set uniques = new HashSet();
	Set dups = new HashSet();
	String[] values = new String[] { "java", "javaexample", "javaexampleshow", "javashow", "java" };
	for (int i = 0; i < values.length; i++)
	    if (!uniques.add(values[i]))
		dups.add(values[i]);
 
	uniques.removeAll(dups); // Destructive set-difference
 
	System.out.println("Unique words:    " + uniques);
	System.out.println("Duplicate words: " + dups);
    }
}

[edit] What Result You Can Get

Run the program, you will get:

Unique words:    [javashow, javaexampleshow, javaexample]
Duplicate words: [java]

[edit] Required External Libraries and/or Files for this Java Example

Need nothing.


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.



[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools