ch
Feedback
Tech Jargon - Decoded

Tech Jargon - Decoded

前往频道在 Telegram

Confused by tech terms? Don’t worry, we’ve got you 🤝 We make things simple, one concept at a time. Learn daily Easy & clear Turn Confusion into clarity. #tech #it #softwareengineer #cs #development

显示更多
2 017
订阅者
无数据24 小时
-77
-4030
帖子存档
Anagram Checker
public class Anagram {
    public static boolean isAnagram(String str1, String str2) {
        if (str1.length() != str2.length()) {
            return false;
        }
        char[] arr1 = str1.toCharArray();
        char[] arr2 = str2.toCharArray();
        java.util.Arrays.sort(arr1);
        java.util.Arrays.sort(arr2);
        return java.util.Arrays.equals(arr1, arr2);
    }

    public static void main(String[] args) {
        String str1 = "listen";
        String str2 = "silent";
        boolean result = isAnagram(str1, str2);
        System.out.println(str1 + " and " + str2 + " are anagrams: " + result);
    }
}

String Comparison in Java: equals() vs. ==
public class StringComparison {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "Hello";
        String str3 = new String("Hello");

        System.out.println("str1 equals str2: " + str1.equals(str2));
        System.out.println("str1 == str2: " + (str1 == str2));
        System.out.println("str1 equals str3: " + str1.equals(str3));
        System.out.println("str1 == str3: " + (str1 == str3));
    }
}

Character Frequency in String
public class CharFrequency {
 public static void main(String[] args) {
 String str = "hello world";
 str = str.toLowerCase();
 int[] freq = new int[26];
 for (int i = 0; i < str.length(); i++) {
 char ch = str.charAt(i);
 if (ch >= 'a' && ch <= 'z') {
 freq[ch - 'a']++;
 }
 }
 for (int i = 0; i < 26; i++) {
 if (freq[i] > 0) {
 System.out.println((char)('a' + i) + ": " + freq[i]);
 }
 }
 }
}

Count Vowels and Consonants in a String
public class VowelConsonantCounter {
    public static void main(String[] args) {
        String str = "Hello, World!";
        str = str.toLowerCase();
        int vowelCount = 0;
        int consonantCount = 0;

        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);

            if (ch >= 'a' && ch <= 'z') {
                if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                    vowelCount++;
                } else {
                    consonantCount++;
                }
            }
        }

        System.out.println("Vowels: " + vowelCount);
        System.out.println("Consonants: " + consonantCount);
    }
}

Palindrome String Checker
public class PalindromeCheck {
    public static boolean isPalindrome(String str) {
        String cleanedStr = str.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
        int left = 0;
        int right = cleanedStr.length() - 1;

        while (left < right) {
            if (cleanedStr.charAt(left) != cleanedStr.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }

    public static void main(String[] args) {
        String testString = "Racecar";
        boolean result = isPalindrome(testString);
        System.out.println("\"" + testString + "\" is a palindrome: " + result);
    }
}

Find the Missing Number in an Array
public class MissingNumber {
    public static void main(String[] args) {
        int[] arr = {1, 2, 4, 6, 3, 7, 8}; 
        int n = 8; 
        int missingNumber = findMissingNumber(arr, n);
        System.out.println("The missing number is: " + missingNumber);
    }

    public static int findMissingNumber(int[] arr, int n) {
        int expectedSum = n * (n + 1) / 2;
        int actualSum = 0;
        for (int num : arr) {
            actualSum += num;
        }
        return expectedSum - actualSum;
    }
}

Move Zeros to End of Array
public class MoveZeros {
    public static void moveZerosToEnd(int[] arr) {
        int n = arr.length;
        int j = 0;
        for (int i = 0; i < n; i++) {
            if (arr[i] != 0) {
                arr[j] = arr[i];
                if (i != j) {
                    arr[i] = 0;
                }
                j++;
            }
        }
    }

    public static void main(String[] args) {
        int[] arr = {0, 1, 0, 3, 12};
        moveZerosToEnd(arr);
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}

Check if Two Arrays are Equal
public class ArrayEquality {
    public static boolean areArraysEqual(int[] arr1, int[] arr2) {
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        int[] array1 = {1, 2, 3, 4, 5};
        int[] array2 = {1, 2, 3, 4, 5};
        int[] array3 = {1, 2, 3, 5, 4};
        boolean equal12 = areArraysEqual(array1, array2);
        boolean equal13 = areArraysEqual(array1, array3);
        System.out.println("array1 and array2 are equal: " + equal12);
        System.out.println("array1 and array3 are equal: " + equal13);
    }
}

Rotate Matrix by 90 Degrees
public class RotateMatrix {
    public static void rotate(int[][] matrix) {
        int n = matrix.length;
        
        for (int i = 0; i < (n + 1) / 2; i++) {
            for (int j = 0; j < n / 2; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[n - 1 - j][i];
                matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j];
                matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i];
                matrix[j][n - 1 - i] = temp;
            }
        }
    }

    public static void printMatrix(int[][] matrix) {
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        int[][] matrix = {{
            1, 2, 3
        }, {
            4, 5, 6
        }, {
            7, 8, 9
        }};

        System.out.println("Original Matrix:");
        printMatrix(matrix);

        rotate(matrix);

        System.out.println("
Rotated Matrix:");
        printMatrix(matrix);
    }
}

Spiral Traversal of a 2D Array
public class SpiralTraversal {
    public static void spiralPrint(int[][] matrix) {
        int startRow = 0, endRow = matrix.length - 1;
        int startCol = 0, endCol = matrix[0].length - 1;

        while (startRow <= endRow && startCol <= endCol) {
            for (int i = startCol; i <= endCol; i++) {
                System.out.print(matrix[startRow][i] + " ");
            }
            startRow++;

            for (int i = startRow; i <= endRow; i++) {
                System.out.print(matrix[i][endCol] + " ");
            }
            endCol--;

            if (startRow <= endRow) {
                for (int i = endCol; i >= startCol; i--) {
                    System.out.print(matrix[endRow][i] + " ");
                }
                endRow--;
            }

            if (startCol <= endCol) {
                for (int i = endRow; i >= startRow; i--) {
                    System.out.print(matrix[i][startCol] + " ");
                }
                startCol++;
            }
        }
    }

    public static void main(String[] args) {
        int[][] matrix = {{1, 2, 3, 4},
                          {5, 6, 7, 8},
                          {9, 10, 11, 12},
                          {13, 14, 15, 16}};

        spiralPrint(matrix);
    }
}

Diagonal Sum of a Square Matrix
public class DiagonalSum {
    public static void main(String[] args) {
        int[][] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };
        int sum = calculateDiagonalSum(matrix);
        System.out.println("Diagonal Sum: " + sum);
    }

    public static int calculateDiagonalSum(int[][] matrix) {
        int sum = 0;
        int n = matrix.length;
        for (int i = 0; i < n; i++) {
            sum += matrix[i][i];
        }
        return sum;
    }
}

Search for an Element in a 2D Matrix
public class MatrixSearch {
 public static boolean searchMatrix(int[][] matrix, int target) {
 int rows = matrix.length;
 int cols = matrix[0].length;
 for (int i = 0; i < rows; i++) {
 for (int j = 0; j < cols; j++) {
 if (matrix[i][j] == target) {
 return true;
 }
 }
 }
 return false;
 }
 public static void main(String[] args) {
 int[][] matrix = {{1, 4, 7, 11, 15},
 {2, 5, 8, 12, 19},
 {3, 6, 9, 16, 22},
 {10, 13, 14, 17, 24},
 {18, 21, 23, 26, 30}};
 int target = 5;
 boolean found = searchMatrix(matrix, target);
 System.out.println("Target found: " + found);
 }
}

Find Largest and Smallest Elements in a 2D Array
public class MatrixMinMax {
 public static void main(String[] args) {
 int[][] matrix = {
 {1, 2, 3},
 {4, 5, 6},
 {7, 8, 9}
 };

 int min = matrix[0][0];
 int max = matrix[0][0];

 for (int i = 0; i < matrix.length; i++) {
 for (int j = 0; j < matrix[i].length; j++) {
 if (matrix[i][j] < min) {
 min = matrix[i][j];
 }
 if (matrix[i][j] > max) {
 max = matrix[i][j];
 }
 }
 }

 System.out.println("Smallest element: " + min);
 System.out.println("Largest element: " + max);
 }
}

Array Sum and Average
public class ArraySumAvg {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};
        int sum = 0;

        for (int i = 0; i < numbers.length; i++) {
            sum += numbers[i];
        }

        double average = (double) sum / numbers.length;

        System.out.println("Sum: " + sum);
        System.out.println("Average: " + average);
    }
}

Copying Array Elements
public class CopyArray {
    public static void main(String[] args) {
        int[] sourceArray = {1, 2, 3, 4, 5};
        int[] destinationArray = new int[sourceArray.length];

        for (int i = 0; i < sourceArray.length; i++) {
            destinationArray[i] = sourceArray[i];
        }

        System.out.print("Destination Array: ");
        for (int i = 0; i < destinationArray.length; i++) {
            System.out.print(destinationArray[i] + " ");
        }
    }
}

Merge Two Arrays
public class MergeArrays {
    public static void main(String[] args) {
        int[] arr1 = {1, 2, 3};
        int[] arr2 = {4, 5, 6};
        int[] mergedArray = new int[arr1.length + arr2.length];

        for (int i = 0; i < arr1.length; i++) {
            mergedArray[i] = arr1[i];
        }

        for (int i = 0; i < arr2.length; i++) {
            mergedArray[arr1.length + i] = arr2[i];
        }

        for (int i = 0; i < mergedArray.length; i++) {
            System.out.print(mergedArray[i] + " ");
        }
    }
}

Remove Duplicate Elements from an Array
public class RemoveDuplicates {
    public static int[] removeDuplicates(int[] arr) {
        if (arr == null || arr.length == 0) {
            return new int[0];
        }

        int n = arr.length;
        int[] temp = new int[n];
        int j = 0;

        for (int i = 0; i < n - 1; i++) {
            if (arr[i] != arr[i + 1]) {
                temp[j++] = arr[i];
            }
        }

        temp[j++] = arr[n - 1];

        int[] result = new int[j];
        for (int i = 0; i < j; i++) {
            result[i] = temp[i];
        }

        return result;
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 2, 3, 4, 4, 4, 5};
        int[] result = removeDuplicates(arr);
        for (int i = 0; i < result.length; i++) {
            System.out.print(result[i] + " ");
        }
    }
}

Count Element Frequencies in an Array
public class FrequencyCounter {
 public static void main(String[] args) {
 int[] arr = {10, 20, 20, 10, 30, 10};
 int n = arr.length;
 int[] frequency = new int[n];
 boolean[] visited = new boolean[n];

 for (int i = 0; i < n; i++) {
 if (visited[i] == true)
 continue;
 int count = 1;
 for (int j = i + 1; j < n; j++) {
 if (arr[i] == arr[j]) {
 visited[j] = true;
 count++;
 }
 }
 frequency[i] = count;
 System.out.println(arr[i] + ": " + count);
 }
 }
}

Matrix Multiplication
public class MatrixMultiplication {
    public static void main(String[] args) {
        int[][] matrix1 = {{1, 2, 3}, {4, 5, 6}};
        int[][] matrix2 = {{7, 8}, {9, 10}, {11, 12}};
        int rows1 = matrix1.length;
        int cols1 = matrix1[0].length;
        int cols2 = matrix2[0].length;
        int[][] result = new int[rows1][cols2];

        for (int i = 0; i < rows1; i++) {
            for (int j = 0; j < cols2; j++) {
                for (int k = 0; k < cols1; k++) {
                    result[i][j] += matrix1[i][k] * matrix2[k][j];
                }
            }
        }

        System.out.println("Resultant Matrix:");
        for (int i = 0; i < rows1; i++) {
            for (int j = 0; j < cols2; j++) {
                System.out.print(result[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Add Two Matrices
public class MatrixAddition {
    public static void main(String[] args) {
        int[][] matrix1 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        int[][] matrix2 = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
        int rows = matrix1.length;
        int cols = matrix1[0].length;

        int[][] sum = new int[rows][cols];

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                sum[i][j] = matrix1[i][j] + matrix2[i][j];
            }
        }

        System.out.println("Sum of the matrices:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                System.out.print(sum[i][j] + " ");
            }
            System.out.println();
        }
    }
}