Count number of binary ones in an Integer using Java

 //To count the number of ones in a Integer we can either use brute force O(n) or //we can ‘AND’ the number with number-1 until num becomes zero //keep a count for how many times the loop runs whic is the ans public class bit {   public static void main(String [] args) { int a = 5; int b = 6; int count = 0; int c; for(c = a ^ b; c != 0; c = c >> 1) { count += c & 1; } System.out.println(count); } }

Check if two Strings are anagrams or not in Java

//Here the main concept is to add the ascii values and if they are same then the strings are anagrams else not

import java.util.*;

public class anagrams {

public static void main(String [] args){

String b = “damma”;

int sum1 =0;

int sum2=0;

char[] c = a.toCharArray();

char[] d = b.toCharArray();

if(c.length == d.length)

{

for (int i =0; i < c.length ; i++)

{

sum1 += c[i];

sum2 += d[i];

}

if(sum1 == sum2){

System.out.println(“They are anagrams”);

}

else{

System.out.println(“They are not”);

}

}

else{

System.out.println(“They are not”);

}

Arrays.sort(c);

Arrays.sort(d);

if(Arrays.equals(c, d) )

{

System.out.println(“They are anagrams”);

}

else{

System.out.println(“They are not “);

}

}

}

Checking for unique charecters in a String using Java

import java.util.HashMap;

//If no addition data structures can be used then we need to comapare each and every charecter with

//all the charectersi in a string

//We can also use the boolean array to check for duplicate charesters in the string

public class uniquestr {

public static void main(String [] args){

String s = “Ishan”;

char[] c = s.toCharArray();

HashMap< Character, Integer> map = new HashMap<Character, Integer>();

for(char i : c){

if(map.containsKey(i))

{

map.put(i, map.get(i) +1);

System.out.println(“The string contains duplicate charecter ” + i);

}

else

map.put(i, 1);

}

}

}

Binary Search program in Java

 //Here you basically take three pointers one at the start of the array one at the end and one at the mid. //Then compare with the element to be found if greater then increment the start to mid+1 and less then //decrement the end to mid -1. Keep going on until start not equal to end. public class arrayserach {   int[]  array = {1,2,3,4,5,6,7,8,9,12,34,345,462,545,777,778,999,1001,1002,1111,1112,1113}; int start, end , mid, count;   public arrayserach(){     start = 0; end = array.length – 1; mid = (array.length/2) – 1; count = 0; }   public int search(int i) {   while(start != end) { count++; if(i ==array[start]) { System.out.println(“Found num: “+i+ “at index: “+start); System.out.println(count); return 1; }   if(i == array[end]){ System.out.println(“Found num: “+i+”at index: “+end); System.out.println(count); return 1;   }   if(i == array[mid]){ System.out.println(“Found num”+i+”at index: “+mid); System.out.println(count); return 1; }   if(array[mid] > i) { end = mid -1; mid = (start + end) / 2; continue; } if(array[mid] < i) { start = mid +1; mid = (start + end) /2; continue; } } if(array[end] == i){ System.out.println(“Found”); System.out.println(count); return 1; } return count; }   }