Question:
Java Program to remove duplicate characters?
?
2009-12-30 08:54:47 UTC
Hi friends,
I need to write a java program using any collections such tht the pgm should accept any no. of repeated letters but finally o/p shudd be non duplicated ones.

for example:
Input "A", "A", "D", "E", "A", "C", "Z", "Y"
Output : "A", "D", "E", "C", "Z", "Y"


Please help me,its of high priority... i'm waiting.


P.S: Happy New Year to all....
Three answers:
SPB
2009-12-30 09:02:05 UTC
Use a Hashtable. Use the letter as the key and the value. Putting "A" in the second time will overwrite the first one, thus eliminating duplicate letters. Of course, order is not retained.



import java.util.Enumeration;

import java.util.Hashtable;

public class NoDups {

public static void main(String[] args) {

String [] input = { "A", "A", "D", "E", "A", "C", "Z", "Y" };

Hashtable letters = new Hashtable();

for (int i = 0; i < input.length; i++) {

letters.put(input[i], input[i]);

}



Enumeration e = letters.keys();

while (e.hasMoreElements()) {

System.out.println(e.nextElement());

}

}

}
deonejuan
2009-12-30 10:11:26 UTC
This one is from the Generics tutorials...



import java.util.*;



public class FindDups {

public static void main(String[] args) {

Set s = new HashSet();

for (String a : args)

if (!s.add(a))

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



System.out.println(s.size() + " distinct words: " + s);

}

}

// java FindDups A A B B E Z Z
2016-05-26 04:42:22 UTC
all i see is ja ja blah blah something - LOL whole nother language!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...