# Capgemini off campus coding problem with solution

Capgemini off campus coding problem solved with complete solution. What kind of coding problems will be asked in capgemini off campus recruitments. How to solve capgemini off campus coding problem.

## Capgemini off campus coding problems

Question:

Given a string write a java algorithm which will do the following tasks on it …

Task 1: Given a string the algorithm should return “Unique Characters” from it.

Example: For input like string s1= “Vrashikesh patil” the output should be  v, r, k, e, p, t, l.

Task 2: Given a string algorithm should return “Duplicate characters” from it.

Example: For input like string s2= Dattatrey patil the expected output would be a,t.

Your task is to write an algorithm which will satisfy both the tasks. Note that the time complexity should be less, that is try to write a code which is efficient in nature.

Algorithm for Task 1 (Unique characters from string )

• Initially take the input from user in form of string
• convert the given string into String buffer.
• The above step is needed because addition and deletion of elements is not possible in string.
• If a character is not repeated then it’s unique, so set one counter to ZERO which is further used to evaluvate the output.
• Using 2 for loops cascaded, the outer for loop is for holding one particular character and inner for loop is used to iterate over other elements of the given string.
• So when outer loop executes for 1 time, the inner loop will be getting executed for n number of times (n indicates the length of given string).
• Inside the inner for loop just check if the elemnts are matching, if true then increase the counter by 1.
• so at the end of inner for loop just check wether counter value is >1, if true then it’s a duplicate element, otherwise it’s a unique element so print it.
• So when the outer for loop gets executed completely all the unique characters will get printed.

Algorithm for Task 2 (Duplicate characters from string )

• Take the input string from user
• Convert given string into string buffer.
• As it will be easy to do manuplations on string buffer.
• now with the help of two for loops iterate through elemnts of given string
• The outer for loop will be pointing the first element, second for loop will iterate through all other elements if there is any match between the current elemnt then increase the count by 1.
• So at the end of inner for loop if the count is greater than 1 then its a duplicate element so print it to output screen.
• Otherwise just got to second element.
• but in above step just to avoid printing the duplicate elemnts again and again make them as ‘o’ (as shown in below java code). This step will make sure that duplicate characters will not be printed.
• So at the end of outer for loop we will be getting all the duplicate elemntsfrom given string.

JAVA CODE

```package arrayPrograms;
import java.util.*;
class StringMethod {

public String getUnique(String str) {

//....CODED BY VRASHIKESH PATIL ....//

String string4="";

StringBuffer sb = new StringBuffer(str);

// Getting duplicate characters from string

for(int i = 0; i< sb.length();i++){
int count1 = 0;
for(int j = i+1;j<sb.length();j++){
if(sb.charAt(i)==sb.charAt(j)){
sb.deleteCharAt(j);
j--;
count1++;
}
}
if(count1 >= 1){

sb.deleteCharAt(i);
i--;
}
}

for(int i=0;i<sb.length();i++) {
string4=string4+sb.charAt(i);
}
return(string4);
}
// getting duplicate elements
public String getDuplicate(String str) {

char string[] = str.toCharArray();
int count;
String string3="";

for(int i = 0; i <string.length; i++) {
count = 1;
for(int j = i+1; j <string.length; j++) {
if(string[i] == string[j] && string[i] != ' ') {
count++;

string[j] = '0';
}
}
if(count > 1 && string[i] != '0') {
string3=string3+string[i];

}
}

return string3;
}
public static void main(String[] args) {
String string1="Doselect is a Great Platform";

StringMethod s1= new StringMethod();

System.out.println(s1.getUnique(string1));

System.out.println(s1.getDuplicate(string1));
}

}
```

Watch complete explanation for above code Here