Remove Duplicate Characters from given string(Java)

Program to print to Remove Duplicate Characters from given string

package InterviewPrograms;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class RemoveDuplicateFromString {
public static void main(String[] …


This content originally appeared on DEV Community and was authored by Suresh Ayyanna

Program to print to Remove Duplicate Characters from given string

package InterviewPrograms;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class RemoveDuplicateFromString {
    public static void main(String[] args) {

        String str = "abbaaca";
        char y[] = str.toCharArray();
        int size = y.length;

        Map<Character, Integer> map = new LinkedHashMap<>();

        int i = 0;
        while (i != size) {
            if (map.containsKey(y[i]) == false) {
                map.put(y[i], 1);
            } else {
                int oldval = map.get(y[i]);
                int newval = oldval + 1;
                map.put(y[i], newval);
            }
            ++i;
        }
        Set<Map.Entry<Character, Integer>> lhmap = map.entrySet();
        String result = "";
        for (Map.Entry<Character, Integer> data : lhmap) {
            result = result + data.getKey();
        }
        System.out.println(result);
    }
}


This content originally appeared on DEV Community and was authored by Suresh Ayyanna


Print Share Comment Cite Upload Translate Updates
APA

Suresh Ayyanna | Sciencx (2022-01-15T17:36:19+00:00) Remove Duplicate Characters from given string(Java). Retrieved from https://www.scien.cx/2022/01/15/remove-duplicate-characters-from-given-stringjava/

MLA
" » Remove Duplicate Characters from given string(Java)." Suresh Ayyanna | Sciencx - Saturday January 15, 2022, https://www.scien.cx/2022/01/15/remove-duplicate-characters-from-given-stringjava/
HARVARD
Suresh Ayyanna | Sciencx Saturday January 15, 2022 » Remove Duplicate Characters from given string(Java)., viewed ,<https://www.scien.cx/2022/01/15/remove-duplicate-characters-from-given-stringjava/>
VANCOUVER
Suresh Ayyanna | Sciencx - » Remove Duplicate Characters from given string(Java). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/15/remove-duplicate-characters-from-given-stringjava/
CHICAGO
" » Remove Duplicate Characters from given string(Java)." Suresh Ayyanna | Sciencx - Accessed . https://www.scien.cx/2022/01/15/remove-duplicate-characters-from-given-stringjava/
IEEE
" » Remove Duplicate Characters from given string(Java)." Suresh Ayyanna | Sciencx [Online]. Available: https://www.scien.cx/2022/01/15/remove-duplicate-characters-from-given-stringjava/. [Accessed: ]
rf:citation
» Remove Duplicate Characters from given string(Java) | Suresh Ayyanna | Sciencx | https://www.scien.cx/2022/01/15/remove-duplicate-characters-from-given-stringjava/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.