Skip to content

Instantly share code, notes, and snippets.

View Ankit-Slnk's full-sized avatar
💭
Coding...

ANKIT SOLANKI Ankit-Slnk

💭
Coding...
View GitHub Profile
@Ankit-Slnk
Ankit-Slnk / sort.dart
Last active December 28, 2024 04:33
Sorts an array of numbers in ascending order in dart.
void bubbleSort(List<int> arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) { // descending order - if (arr[j] < arr[j + 1]) {
// Swap the elements
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
@Ankit-Slnk
Ankit-Slnk / countCharacterOccurrences.dart
Created December 28, 2024 04:29
Count the occurrences of each character in the string in dart.
Map<String, int> countCharacterOccurrences(String str) {
Map<String, int> charCount = {};
for (int i = 0; i < str.length; i++) {
String char = str[i]; // Get each character in the string
// If the character is already in the map, increment its count; otherwise, add it with a count of 1
charCount[char] = (charCount[char] ?? 0) + 1;
}
return charCount;
@Ankit-Slnk
Ankit-Slnk / prime.dart
Created December 28, 2024 04:27
Check if a given number is prime in dart.
bool isPrime(int n) {
// Edge case: numbers less than or equal to 1 are not prime
if (n <= 1) {
return false;
}
// Check if n is 2 (the only even prime number)
if (n == 2) {
return true;
}
@Ankit-Slnk
Ankit-Slnk / mergeSortedArrays.dart
Created December 28, 2024 04:16
Given 2 arrays that are sorted, merge them and sort in dart.
List<int> mergeSortedArrays(List<int> arr1, List<int> arr2) {
int i = 0; // Pointer for arr1
int j = 0; // Pointer for arr2
List<int> result = [];
// Merge both arrays
while (i < arr1.length && j < arr2.length) {
if (arr1[i] < arr2[j]) {
result.add(arr1[i]);
i++;
@Ankit-Slnk
Ankit-Slnk / factorial.dart
Created December 28, 2024 04:11
Factorial of given number in dart
int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
void main() {
int num = 5; // You can change this number to test with others
@Ankit-Slnk
Ankit-Slnk / reverse_string.dart
Created December 28, 2024 04:09
Reverse of a string without using built-in method in dart
String reverseString(String str) {
String reversed = ''; // Initialize an empty string to store the reversed string.
// Iterate through the string in reverse order
for (int i = str.length - 1; i >= 0; i--) {
reversed += str[i]; // Add each character to the reversed string.
}
return reversed; // Return the reversed string.
}
@Ankit-Slnk
Ankit-Slnk / palindrome.dart
Created December 28, 2024 04:06
Check whether a string is palindrome or not
bool isPalindrome(String str) {
// Remove spaces and convert the string to lowercase for case-insensitive comparison
String cleanedStr = str.replaceAll(RegExp(r'\s+'), '').toLowerCase();
// Reverse the string and compare it with the original string
String reversedStr = cleanedStr.split('').reversed.join('');
return cleanedStr == reversedStr;
}
@Ankit-Slnk
Ankit-Slnk / longest_word_in_sentence.dart
Created December 28, 2024 04:03
Find longest word in a given sentence in dart?
void main() {
String sentence = "Dart is a programming language for building apps";
List<String> words = sentence.split(' '); // Split by spaces
// Initialize the longest word as an empty string
String longestWord = '';
// Iterate through the words and find the longest one
for (String word in words) {
@Ankit-Slnk
Ankit-Slnk / fibonacci_series.dart
Created December 28, 2024 03:59
Fibonacci series in dart
void main() {
int a=0;
int b=1;
print(a);
print(b);
for(int i=1;i<10; i++){
int c=a+b;
print(c);
a=b;
b=c;
@Ankit-Slnk
Ankit-Slnk / DecimalFormat.java
Created March 15, 2023 12:00
Fix decimal format for price
public static String getDecimalFormat(double value) {
String newValue = new DecimalFormat("#.##").format(value);
if (!newValue.contains(".")) {
return newValue + ".00";
} else {
return newValue.split("\\.")[1].length() == 1 ? (newValue + "0") : newValue;
}
}