Remove duplicates from String without Collection API

In this post I am placing one of the challenge which I have faced in Interview and even online coding test for one of the company. Remove duplicate characters / words from String without using Collection API? is also a common interview question for most of the interview attendees in Java technology. The question may ask different like find duplicate character count from given string? Here are the few different solutions I am placing, one among them will be suitable for you. If you haven’t find solution for your problem, please write your problem in comments so I will make some solution ready and post for you.

Problem: Find each character how many times repeated in given String without Collection API?

package com.challenges.string;
public class RepeatedCountOfCharacterInStringWithoutCollections {
    public static void main(String[] args) {
 String str = "javaisocean";
 int count = 0;
 if (str != null) {
            System.out.println();
            System.out.println("Each Character repeated in given String is: ");
     for (int i = 0; i < str.length(); i++) {
  char initialChar = str.charAt(i);
  int length = str.length();
  str = str.replaceAll(str.charAt(i) + "", "");
  count = length - str.length();
              System.out.println(initialChar + " repeated at " + count);
  i--;
     }
 }
    }
}

Output:
Each Character repeated in given String is: 
j repeated at 1
a repeated at 3
v repeated at 1
i repeated at 1
s repeated at 1
o repeated at 1
c repeated at 1
e repeated at 1
n repeated at 1

Problem: Remove duplicate character from given String without using Collection API?

package com.challenges.string;
public class RemoveDuplicateCharactersInStringWithoutCollection {
    public static void main(String[] args) {
 String str = "javaisocean";
 int count = 0;
 String finalString = "";
 if (str != null) {
     for (int i = 0; i < str.length(); i++) {
  char initialChar = str.charAt(i);
  int length = str.length();
  str = str.replaceAll(str.charAt(i) + "", "");
  count = length - str.length();
  if (count == 1)
      finalString = finalString + initialChar + "";
  i--;
      }
 System.out.println("Unique Character in String is: " + finalString);
 }
    }
}

Output:
Unique Character in String is: jvisocen

Problem: Find repeated words in given String with repeated count without using Collection API?

package com.challenges.string;
public class FindRepeatedWordsInStringWithoutCollectionAPI {
    public static void main(String[] args) {
 String str = "Subbareddy Nallamachu Nallamachu Subbareddy Java";
 int count = 0;
 if (str != null) {
      System.out.println();
      System.out.println(
                  "Repeated words in given String with repeted count is: ");
             str = str.toLowerCase();
      String[] arStr = str.split(" ");
      for (int i = 0; i < arStr.length; i++) {
  String s = arStr[i];
  int length = str.length();
  str = str.replaceAll(s, "");
  count = length - str.length();  
  if (count > s.length()) {
        int repeatedCount = count/s.length();
               System.out.println(s+ " "+repeatedCount);
  }
     }
  }
    }
}
 
Output:
Repeated words in given String with repeted count is: 
subbareddy 2
nallamachu 2

Requesting you to place a comment which you have faced challenges. Thanks for vising. Happy Learning… 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s