TypeScript Coding
Syntax
1. string and char
let strInput: string = "abc";
let strToCharArray = strInput.split("");
console.log("string to char array is ", strToCharArray);
console.log("capitalize first letter", strToCharArray[0].toUpperCase())
let charArrayToStr = strToCharArray.join("");
console.log("char array to string is ", charArrayToStr);
2. array problem
2.1 find if duplicate persent
console.log(‘find if duplicate persent');
let arr: number[] = [1, 2, 3, 2, 4, 5, 2, 4];
let n = arr.length;
let visited: boolean[] = new Array(n).fill(false);
for (let i = 0; i < n; i++) {
if (visited[i]) {
continue;
let isDuplicate: boolean = false;
for (let j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
isDuplicate = true;
visited[j] = true;
if (isDuplicate) {
console.log("duplicates are",arr[i]);
2.2 Remove duplicates from array
console.log('remove duplicates from array');
let numberArr: number[] = [1, 2, 3, 4, 1, 2];
let n: number = numberArr.length;
let visited: boolean[] = new Array(n).fill(false);
let uniqueArr: number[] = [];
for (let i = 0; i < n; i++) {
if (visited[i]) {
continue;
}
for (let j = i + 1; j < n; j++) {
if (numberArr[i] == numberArr[j]) {
visited[j] = true;
}
}
uniqueArr.push(numberArr[i]);
}
for (let item of uniqueArr) {
console.log("unique elements", item);
}
3. dictionary problem
3.1 Occurrence of each number
console.log('count occurence');
let numberArr: number[] = [1, 2, 3, 4, 1, 2];
let n: number = numberArr.length;
let dict: { [key: number]: number } = {};
for (let item of numberArr) {
if (dict.hasOwnProperty(item)) {
dict[item]++;
else {
dict[item] = 1;
console.log('display dictionary items using for..in');
for (let key in dict) {
console.log('Number - ' + key + ' & its ' + 'Occurence - ' + dict[key]);
console.log('display dictionary items using Object entries');
Object.entries(dict).forEach(([key, value]) => {
console.log('Number - ' + key + ' & its ' + 'Occurence - ' + value);
});
console.log('display dictionary items using Object entries after filtering it');
Object.entries(dict).filter(([key, value]) => value > 1).forEach(([key, value]) => {
console.log('Number - ' + key + ' & its ' + 'Occurence - ' + value);
});
Coding Problems
1. Write a function that counts the number of vowels in a
string.
//Write a function that counts the number of vowels in a string.
let result= countvowels("Hello world");
function countvowels(strInput:string): number {
//let strInput: string = "Hello world";
let vowels: string = "aeiouAEIOU";
let vowelcnt: number = 0;
for (let ch of strInput) {
if (vowels.includes(ch)) {
vowelcnt++;
return vowelcnt;
}
console.log("count of vowels in string is :", result);
2. Write a palindrome program(solve it
without converting the integer to a string) in
any online editor
3. Union, Union all, Intersect, except
Union -
function union<T>(arr1: T[], arr2: T[]): T[] {
return [...new Set([...arr1, ...arr2])];
const array1 = [1, 2, 3, 4];
const array2 = [3, 4, 5, 6];
console.log(union(array1, array2)); // Output: [1, 2, 3, 4, 5, 6]
union all -
function unionAll<T>(arr1: T[], arr2: T[]): T[] {
return [...arr1, ...arr2]; // No duplicate removal
console.log(unionAll(array1, array2)); // Output: [1, 2, 3, 4, 3, 4, 5, 6]