This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void printCharacterCount(String str) { | |
// String str = "I am a Java Developer"; | |
// Result map to print the final result | |
Map<Character, Integer> count = new HashMap<>(); | |
// Removing All Spaces in a String | |
String noSpaceStr = str.replaceAll("\\s+", "").toLowerCase(Locale.ROOT); | |
for (int i = 0; i < noSpaceStr.length() - 1; i++) { | |
count.computeIfPresent(noSpaceStr.charAt(i), (key, value) -> value + 1); | |
count.putIfAbsent(noSpaceStr.charAt(i), 1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static java.lang.reflect.Array.getChar; | |
public static int hashCode(byte[] value) { | |
int h =0; | |
int length = value.length >> 1; | |
for (int i = 0; i < length; i++) { | |
h = 31 * h + getChar(value, i); | |
} | |
return h; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun isPalinDrome(no: Int) { | |
var temp = no | |
var newNo = 0 | |
while (temp > 0) { | |
val r = temp % 10 | |
newNo = (newNo * 10) + r | |
temp /= 10 | |
} | |
if (no == newNo) { | |
println("$no is Palindrome") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun isPalinDrome(str:String) { | |
var start = 0 | |
var end = str.length-1 | |
var isPalinDrome = true | |
while(start < end) { | |
if(str[start]!= str[end]){ | |
isPalinDrome = false | |
} | |
start++ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun numberOfVCInString(str:String) { | |
var vowelsCount = 0 | |
var consonantsCount = 0 | |
val vowels = arrayOf('a','e','i','o','u') | |
for(ch in str.toLowerCase()) { | |
if(ch in vowels) { | |
vowelsCount ++ | |
} else if(ch in 'a'..'z' && ch !in vowels) { | |
consonantsCount ++ | |
} else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun reverseString(str:String) { | |
var newStr = "" | |
var length = str.length-1 | |
while(length >= 0){ | |
newStr += str[length] | |
length -- | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun reverseString(str:String) { | |
var newStr = "" | |
for(i in str.length-1 downTo 0) { | |
newStr += str[i] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun isNumberOddOrEven(n : Int) { | |
if(n%2==0){ | |
println("$n is Even Number") | |
} else { | |
println("$n is Odd Number") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun printFibonacciNumbersUptoN (n : Int) { | |
var firstNo = 0 | |
var secondNo = 1 | |
while(firstNo <= n) { | |
print("$firstNo, ") | |
var nextNo = firstNo + secondNo | |
firstNo = secondNo | |
secondNo = nextNo | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun printNFibonacciNumbers(n : Int) { | |
var firstNo = 0 | |
var secondNo = 1 | |
var i = 1 | |
while(i<=n) { | |
print("$firstNo, ") | |
var nextNo = firstNo + secondNo | |
firstNo = secondNo | |
secondNo = nextNo | |
i++ |
NewerOlder