public void replaceWhiteSpaceMethod1(String str) {
// Method 1
System.out.println(str.replaceAll(" ", ""));
// Method2
char[] arr = str.toCharArray();
for (char c : arr) {
if (c != ' ')
System.out.print(c);
}
}
public void findTheNumberOfDublicates(String str){
Map hm = new HashMap();
char[] arr = str.toCharArray();
for(char c : arr){
if(hm.containsKey(c)){
int val = hm.get(c);
hm.put(c, (val+1));
}else{
hm.put(c, 1);
}
}
System.out.println(hm);
Set s = hm.entrySet();
Iterator i = s.iterator();
while(i.hasNext()){
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + ": "+me.getValue() +",");
}
for(Entry e: hm.entrySet()){
System.out.print(e.getKey() + " "+e.getValue()+",");
}
}
/*Given int a[]={3,6,6,3,9,3,5,7,2,5}; and target 12. Sum of pairs equals 12 is below
{(6,6) (6,6) (3,9) (9,3) (5,7) (7,5) } . Pair (3,9) and (9,3) will be treated same so answer is 3.*/
public class SumOfPairs {
public static void main(String[] args) {
// TODO Auto-generated method stub
int target = 12;
int a[]={3,6,6,3,9,3,5,7,2,5};
SumOfPairs obj = new SumOfPairs();
System.out.println(obj.pairs(a, target));
}
public int pairs(int[] a, int target){
int flag =0;
ArrayList> al = new ArrayList>();
for(int i=0;i pair = new ArrayList();
ArrayList repair = new ArrayList();
pair.add(a[i]);
pair.add(a[i+1]);
repair.add(a[i+1]);
repair.add(a[i]);
if(al.contains(pair)||al.contains(repair)){
}else{
al.add(pair);
al.add(repair);
flag++;
}
}
}
for(int i=0;i
No comments:
Post a Comment