Arrays and Strings - Day 74 : Harshil Chovatiya

Day 74 : Arrays and Strings - Harshil Chovatiya

Day 74: Arrays and Strings

Brief:

On Day 74, we delve into the fundamental data structures of arrays and strings. These structures are ubiquitous in programming and are essential building blocks for more complex data structures and algorithms. We'll explore their characteristics, operations, and common algorithms, equipping ourselves with the necessary tools to manipulate and utilize them effectively in problem-solving.

Arrays and Strings - Day 74 : Harshil Chovatiya

Topics Covered:

1. Arrays:

  • Definition: Arrays are sequential collections of elements, each identified by an index.
  • Operations: Accessing elements by index, inserting elements, deleting elements, and updating elements.
  • Example: Storing exam scores of students in an array and performing operations like finding the maximum score or calculating the average score.

2. Strings:

  • Definition: Strings are sequences of characters, often used to represent text.
  • Operations: Concatenation, substring extraction, searching for patterns, and string manipulation.
  • Example: Manipulating strings to reverse their order, find occurrences of a specific character, or extract substrings.

3. Array and String Algorithms:

  • Searching Algorithms:
    • Linear Search: Iteratively checks each element until the target is found.
    • Binary Search: Efficiently searches a sorted array by dividing it in half at each step.
  • Sorting Algorithms:
    • Selection Sort: Finds the minimum element and swaps it with the first unsorted element.
    • Insertion Sort: Builds the sorted array one element at a time by repeatedly taking the next element and inserting it into the correct position.

Example:

Let's consider an example where we have an array of exam scores and a string representing a sentence:

                
        // Array of exam scores
        let scores = [85, 92, 78, 95, 88];
        
        // String representing a sentence
        let sentence = "The quick brown fox jumps over the lazy dog";
                
            

1. Array Operations Example:

                
        // Find the maximum score
        let maxScore = Math.max(...scores);
        console.log("Maximum score:", maxScore);
                
            

2. String Operations Example:

                
        // Reverse the sentence
        let reversedSentence = sentence.split('').reverse().join('');
        console.log("Reversed sentence:", reversedSentence);
                
            

3. Searching and Sorting Example:

                
        // Use binary search to find a score in the array
        function binarySearch(arr, target) {
            let left = 0;
            let right = arr.length - 1;
            while (left <= right) {
                let mid = Math.floor((left + right) / 2);
                if (arr[mid] === target) {
                    return mid;
                } else if (arr[mid] < target) {
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }
            return -1;
        }
        
        let index = binarySearch(scores, 78);
        console.log("Index of score 78:", index);
                
            

Conclusion:

Day 74 has provided a comprehensive understanding of arrays and strings, along with common operations and algorithms associated with them. Armed with this knowledge, we're better equipped to tackle a wide range of problems that involve these fundamental data structures. As we proceed, remember to leverage the versatility of arrays and strings creatively in problem-solving scenarios.

Social Media

Comments