Dart Coding Questions and Solutions
1. Check if Number is Even or Odd
bool isEven(int number) {
return number % 2 == 0;
}
2. Find the Largest Number in a List
int findLargest(List<int> numbers) {
return numbers.reduce((a, b) => a > b ? a : b);
}
3. Sum of All Elements in a List
int sumList(List<int> numbers) {
return numbers.reduce((a, b) => a + b);
}
4. Find the Smallest Number in a List
int findSmallest(List<int> numbers) {
return numbers.reduce((a, b) => a < b ? a : b);
}
5. Reverse a String
String reverseString(String s) {
return s.split('').reversed.join('');
}
6. Check if a Number is Prime
bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= n ~/ 2; i++) {
if (n % i == 0) return false;
}
return true;
}
7. Calculate the Factorial of a Number
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
8. Find the Greatest Common Divisor (GCD)
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
9. Find the Least Common Multiple (LCM)
int lcm(int a, int b) {
return (a * b) ~/ gcd(a, b);
}
10. Remove Duplicates from a List
List<int> removeDuplicates(List<int> arr) {
return arr.toSet().toList();
}
11. Check if a String is a Palindrome
bool isPalindrome(String s) {
String reversed = s.split('').reversed.join('');
return s == reversed;
}
12. Find the Second Largest Number in a List
int secondLargest(List<int> arr) {
arr.sort();
return arr[arr.length - 2];
}
13. Find the Longest Substring without Repeating Characters
String longestSubstringWithoutRepeating(String s) {
String longest = '';
String current = '';
for (var char in s.split('')) {
if (!current.contains(char)) {
current += char;
} else {
if (current.length > longest.length) longest = current;
current = char;
}
}
return current.length > longest.length ? current : longest;
}
14. Count the Frequency of Characters in a String
Map<String, int> characterFrequency(String s) {
Map<String, int> freq = {};
for (var char in s.split('')) {
freq[char] = (freq[char] ?? 0) + 1;
}
return freq;
}
15. Find the Intersection of Two Lists
List<int> intersection(List<int> arr1, List<int> arr2) {
return arr1.where((element) => arr2.contains(element)).toList();
}
16. Find the Union of Two Lists
List<int> union(List<int> arr1, List<int> arr2) {
return (arr1 + arr2).toSet().toList();
}
17. Convert Decimal to Binary
String decimalToBinary(int decimal) {
return decimal.toRadixString(2);
}
18. Calculate the Sum of a Geometric Progression
double sumOfGeometricProgression(int a, int r, int n) {
return a * (1 - pow(r, n)) / (1 - r);
}
19. Find the Maximum Difference in a List
int maxDifference(List<int> arr) {
int min = arr[0];
int maxDiff = 0;
for (int i = 1; i < arr.length; i++) {
maxDiff = max(maxDiff, arr[i] - min);
min = min(arr[i], min);
}
return maxDiff;
}
20. Find Pairs of Numbers that Sum to a Target
List<List<int>> findPairs(List<int> arr, int target) {
List<List<int>> pairs = [];
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] + arr[j] == target) {
pairs.add([arr[i], arr[j]]);
}
}
}
return pairs;
}
21. Rotate a List
List<int> rotateList(List<int> arr, int k) {
k = k % arr.length;
return arr.sublist(k)..addAll(arr.sublist(0, k));
}
22. Flatten a Nested List
List flatten(List nestedList) {
return nestedList.expand((element) => element is List ? flatten(element) :
[element]).toList();
}
23. Count Occurrences of a Substring
int countOccurrences(String s, String sub) {
int count = 0;
int index = s.indexOf(sub);
while (index != -1) {
count++;
index = s.indexOf(sub, index + sub.length);
}
return count;
}
24. Merge Two Sorted Lists
List<int> mergeSortedLists(List<int> list1, List<int> list2) {
List<int> result = [];
int i = 0, j = 0;
while (i < list1.length && j < list2.length) {
if (list1[i] <= list2[j]) {
result.add(list1[i]);
i++;
} else {
result.add(list2[j]);
j++;
}
}
result.addAll(list1.sublist(i));
result.addAll(list2.sublist(j));
return result;
}
25. Find the Most Frequent Word in a String
String mostFrequentWord(String s) {
List<String> words = s.split(' ');
Map<String, int> freq = {};
for (var word in words) {
freq[word] = (freq[word] ?? 0) + 1;
}
return freq.keys.reduce((a, b) => freq[a]! > freq[b]! ? a : b);
}
26. Check if a Number is a Perfect Square
bool isPerfectSquare(int n) {
int sqrtN = sqrt(n).toInt();
return sqrtN * sqrtN == n;
}