- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to find duplicate characters in a String?
Following is a Java example which deletes duplicate elements from a given string.
Example
import java.util.Arrays; import org.apache.commons.lang3.ArrayUtils; public class DuplicateSample { public static void main(String args[]){ String str = "malayalam"; char[] myArray = str.toCharArray(); for(int i=0; i<myArray.length-1; i++){ for (int j=i+1; j<myArray.length; j++){ if(myArray[i] == myArray[j]){ myArray = ArrayUtils.remove(myArray, j); } } } System.out.println("String value after deleting the duplicate values :"+Arrays.toString(myArray)); } }
Output
String value after deleting the duplicate values :[m, a, l, y]
Advertisements