Programming tips for solving problems
Imagine programming is mathematics. In mathematics, the more you know about the rules the more you become good at it. Similarly, the more rules we know about programming the more we become good at it.
Following are some rules about programming for solving problems:
How to find even and odd numbers?
Following are a few techniques for finding even and odd numbers:
By using modulus
const isEven = (n) => n % 2 == 0 const isOdd = (n) => Math.abs(n % 2) == 1
By using a bit test
const isEven = (n) => ~n & 1 const isOdd = (n) => n & 1
How to count the total bits of a decimal number?
Following is a function to count the bit of a decimal number using the right shift operator:
// 4 = 100, The total bit count is 1 // 7 = 111, The total bit count is 3 const countBit = (num) => { let sum = 0; while(num) { sum += num & 1; num = num >> 1; } return sum; };
Useful rules of XOR?
The XOR with 0 and any number is equal to the number itself. 5 ^ 0 = 5
The XOR of any number with itself is equal to 0. 5 ^ 5 = 0
XOR is commutative, which means the order exchange will result in the same value. 5 ^ 4 = 4 ^ 5
Get the frequency of an array of numbers using Map
const getFreqMap = (item) => { const map = new Map(); for(let i = 0; i < item.length; i++) { map.set(item[i], map.has(item[i]) ? map.get(item[i]) + 1 : 1); } return map; }
Get intersection of two arrays
// Approach #1: function intersection(nums1, nums2) { const set = new Set(nums1); return [...new Set(nums2.filter(n => set.has(n)))]; } // Approach #2: const intersect = arr2.map((x) => arr1.filter((y) => y == x)).flat();
Delete or remove array element by value
const removeElm = (arr, item) => { const index = arr.indexOf(item); if (index !== -1) { arr.splice(index, 1); } }
Check a char is a letter
const isLetter = (l) => { return l.toLowerCase() !== l.toUpperCase(); }
Bit count of an integer number
const bitCount = (n) => { return n.toString(2).match(/1/g).length; }
Check if a number is prime or not
const isPrime = num => { for(let i = 2, s = Math.sqrt(num); i <= s; i++) if(num % i === 0) return false; return num > 1; }
Check if the char is a vowel or not
const isVowel = (c) => { return ['a', 'e', 'i', 'o', 'u'].indexOf(c.toLowerCase()) !== -1 };
Find second highest number in an array of numbers
var secondHighest = function(s) { let firstLarge = -1, secondLarge = -1; for (let i = 0; i < s.length; i++) { const num = s[i]; if (num > firstLarge) { secondLarge = firstLarge; firstLarge = num; } else if (num !== firstLarge && num > secondLarge) { secondLarge = num; } } return secondLarge; }; console.log(secondHighest([5, 13, 27, 9, 63, 63])) // 27
Is upper case or lower case letter in JavaScript
const isUpperCase = (c) => c === c.toUpperCase(); const isLowerCase = (c) => c === c.toLowerCase();
Check if a char is numeric or not
const isNumeric => (n) { return !isNaN(parseFloat(n)) && isFinite(n); }
Get the digit count of a number
let num = 123; let sum = 0; while(num > 0) { sum += num % 10; num = Math.floor(num / 10); }
In-Place number swapping using XOR
let x = 10, y = 5; // 'x' binary (1010) and 'y' binary (0101) x = x ^ y; // x now becomes 15 (1111) y = x ^ y; // y becomes 10 (1010) x = x ^ y; // x becomes 5 (0101)