This file contains hidden or 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 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; | |
| } |
This file contains hidden or 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
| 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; |
This file contains hidden or 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
| 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; | |
| } |
This file contains hidden or 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
| 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++; |
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| 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. | |
| } |
This file contains hidden or 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
| 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; | |
| } |
This file contains hidden or 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 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) { |
This file contains hidden or 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 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; |
This file contains hidden or 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
| 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; | |
| } | |
| } |
NewerOlder