You are on page 1of 2

To Reove duplicates from String:

-------------------------------
public class test {

public static void main(String[] args) {

String input = new String("abbc");


String output = new String();

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


for (int j = 0; j < output.length(); j++) {
if (input.charAt(i) != output.charAt(j)) {
output = output + input.charAt(i);
}
}
}

System.out.println(output);

}
Output:abc
To Print no of occurences of each character in String in Java:
--------------------------------------------------------------
public class occurenceOfCharacter {
public static void main(String[] args) {
String str = "SSDRRRTTYYTYTR";
HashMap <Character, Integer> hMap = new HashMap<>();
for (int i = str.length() - 1; i > = 0; i--) {
if (hMap.containsKey(str.charAt(i))) {
int count = hMap.get(str.charAt(i));
hMap.put(str.charAt(i), ++count);
} else {
hMap.put(str.charAt(i),1);
}
}
System.out.println(hMap);
}
}
output:
-------
{D=1, T=4, S=2, R=4, Y=3}
public class SmallestNumberInAnArray {
public static void main(String args[]){
int temp, size;
int array[] = {10, 20, 25, 63, 96, 57};
size = array.length;

for(int i = 0; i<size; i++ ){


for(int j = i+1; j<size; j++){
if(array[i]>array[j]){
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
System.out.println("2nd Smallest element of the array is:: "+array[0]);
}
}
public class FindBiggestSmallestNumber {

public static void main(String[] args) {


int numbers[] = new int[]{33,53,73,94,22,45,23,87,13,63};
int smallest = numbers[0];
int biggest = numbers[0];

for(int i=1; i< numbers.length; i++)


{
if(numbers[i] > biggest)
biggest = numbers[i];
else if (numbers[i] < smallest)
smallest = numbers[i];

System.out.println("Largest Number is : " + biggest);


System.out.println("Smallest Number is : " + smallest);
}
}

You might also like