Dart Basic Array String Questions
Dart Basic Array String Questions
==================================================================
return result;
}
3. Remove Duplicates
---------------------
List<int> removeDuplicates(List<int> arr) {
List<int> unique = [];
for (int i = 0; i < arr.length; i++) {
bool found = false;
for (int j = 0; j < unique.length; j++) {
if (arr[i] == unique[j]) {
found = true;
break;
}
}
if (!found) unique.add(arr[i]);
}
return unique;
}
4. Sum of Elements
-------------------
int sumArray(List<int> arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) sum += arr[i];
return sum;
}
6. Reverse String
------------------
String reverseString(String str) {
String reversed = '';
for (int i = str.length - 1; i >= 0; i--) reversed += str[i];
return reversed;
}
7. Count Vowels
----------------
int countVowels(String str) {
int count = 0;
String vowels = "aeiouAEIOU";
for (int i = 0; i < str.length; i++) {
for (int j = 0; j < vowels.length; j++) {
if (str[i] == vowels[j]) {
count++;
break;
}
}
}
return count;
}
8. Check Palindrome
--------------------
bool isPalindrome(String str) {
int start = 0, end = str.length - 1;
while (start < end) {
if (str[start].toLowerCase() != str[end].toLowerCase()) return false;
start++;
end--;
}
return true;
}
9. Char Frequency
------------------
Map<String, int> charFrequency(String str) {
Map<String, int> freq = {};
for (int i = 0; i < str.length; i++) {
String char = str[i];
if (freq.containsKey(char)) freq[char] = freq[char]! + 1;
else freq[char] = 1;
}
return freq;
}