package Interview;
import java.util.ArrayList;
public class uniqueChar {
public static void main(String[] args) {
String name = "aaddjlsjjllareer";
int nameLgth = name.length();
String charAti ;
String charAtj ;
int count;
ArrayList<String> alist = new ArrayList<>();
ArrayList<String> blist = new ArrayList<>();
ArrayList<String> clist = new ArrayList<>();
for(int i=0;i<nameLgth;i++) {
charAti = Character.toString(name.charAt(i));
count=0;
for(int j=0;j<nameLgth;j++) {
charAtj = Character.toString(name.charAt(j));
if(charAti.equals(charAtj)) {
count++;
}
}
alist.add(Integer.toString(count).concat(charAti));
blist.add(Integer.toString(count));
clist.add(charAti);
}
System.out.println("alsit is "+ alist); //[3a, 3a, 2d, 2d, 3j, 3l, 1s, 3j, 3j, 3l, 3l, 3a, 2r, 2e, 2e, 2r]
System.out.println("blsit is "+ blist); //[3, 3, 2, 2, 3, 3, 1, 3, 3, 3, 3, 3, 2, 2, 2, 2]
System.out.println("clsit is "+ clist); //[a, a, d, d, j, l, s, j, j, l, l, a, r, e, e, r]
String uniqueElement = null;
for(int i=0;i<nameLgth;i++) {
String uniqueNum = blist.get(i);
if(uniqueNum.equals("1")){
uniqueElement = clist.get(i);
}
}
System.out.println("Unique Element is "+ uniqueElement);
}}
Output:
alsit is [3a, 3a, 2d, 2d, 3j, 3l, 1s, 3j, 3j, 3l, 3l, 3a, 2r, 2e, 2e, 2r]
blsit is [3, 3, 2, 2, 3, 3, 1, 3, 3, 3, 3, 3, 2, 2, 2, 2]
clsit is [a, a, d, d, j, l, s, j, j, l, l, a, r, e, e, r]
Unique Element is s
No comments:
Post a Comment