character array question
1 Answer
character array question
| Flag | PURGE
Write a java Program such that Given two character arrays a[] and b[], remove from b[] all occurrences of all characters that occur in array a[]. You need to do this in-place i.e. without using an extra array of characters. E.g.:
Input: char a[] = {'G', 'O'}
Input char b[] = {'G', 'O', 'O', 'G', 'L', 'E'}
Output: char b[] = {'L', 'E'}
Please provide me with the java code for the same.
Email me when people comment.
Email me when people comment.
Loading...
An error occurred in subscribing you.
Email me when people comment.
Email me when people comment.
Loading...
An error occurred in subscribing you.
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
import java.util.Arrays;
- cCAACc May 05, 2013public class TestLoop {
public static void main(String[] args) {
char[] a = {'G','O'};
char[] b = {'G','O','O','G','L','E'};
//remove the duplicate as {' ',' ',' ',' ','L','E'};
for(int i=0;i<a.length;i++){
for(int j=0;j<b.length;j++){
if(a[i]==b[j]){
b[j]=' ';
}
}
}
// move the ' ' to end.. as {'L','E',' ',' ',' ',' '}
for(int k=0;k<b.length ;k++){
for(int l=k+1;l<b.length;l++)
if(b[k] == ' ' && b[l] != ' ' ){
b[k] = b[l];
b[l] = ' ';
}
}
// now search the first position of ' ' and create sub array from 0 to ii th position
for(int ii=0;ii<b.length;ii++){
if(b[ii] == ' '){
b = Arrays.copyOfRange(b, 0, ii);
break;
}
}
for(int kk=0;kk<b.length;kk++)
System.out.println("-----------:" + b[kk]);
}
}