Programming-Basics

Below you will find pages that utilize the term “Programming-Basics”
October 19, 2024
LRU Cache Implementation in Swift

An LRU (Least Recently Used) Cache is a data structure that keeps track of the most recently used items and efficiently removes the least recently used ones when it reaches capacity. It’s commonly used to optimize memory usage in systems that handle large data sets by retaining only frequently accessed items.

September 15, 2023
Search Insert Position - LeetCode : Swift

Search Insert Position - LeetCode

Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

September 14, 2023
Find Peak Element - LeetCode : Swift

Find Peak Element - LeetCode

A peak element is an element that is strictly greater than its neighbors.

Given a 0-indexed integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks.

September 13, 2023
Search a 2D Matrix - LeetCode : Swift

Search a 2D Matrix - LeetCode

You are given an m x n integer matrix matrix with the following two properties:

September 12, 2023
Length of Last Word - LeetCode : Swift

Length of Last Word - LeetCode

Given a string s consisting of words and spaces, return the length of the last word in the string. A word is a maximal substring consisting of non-space characters only.

September 11, 2023
Roman to Integer - LeetCode : Swift

Roman to Integer - LeetCode

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

September 10, 2023
Remove Duplicates from Sorted Array II - LeetCode : Swift

Remove Duplicates from Sorted Array || - LeetCode

Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same.

September 9, 2023
Best Time to Buy and Sell Stock - LeetCode : Swift

Best Time to Buy and Sell Stock - LeetCode

You are given an array prices where prices[i] is the price of a given stock on the ith day.

September 8, 2023
Rotate Array - LeetCode : Swift

Rotate Array - LeetCode

Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.

September 6, 2023
Remove Duplicates from Sorted Array - LeetCode : Swift

Remove Duplicates from Sorted Array - LeetCode

Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums.

September 5, 2023
Remove Element - LeetCode : Swift

Remove Element - LeetCode

Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val.

September 4, 2023
Merge Sorted Array - LeetCode : Swift

Merge Sorted Array - LeetCode

You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.

September 3, 2023
Next Greater Element I - LeetCode : Swift

Next Greater Element I - LeetCode

The next greater element of some element x in an array is the first greater element that is to the right of x in the same array.

September 2, 2023
Longest Common Prefix - LeetCode : Swift

Longest Common Prefix - LeetCode

Write a function to find the longest common prefix string amongst an array of strings.

September 1, 2023
Two Sum - LeetCode : Swift

Two Sum - LeetCode

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

July 18, 2023
Bubble Sort : Swift

Bubble sort is a simple and straightforward sorting algorithm commonly used in computer science. In Swift language, Bubble sort works by repeatedly swapping adjacent elements if they are in the wrong order until the entire list is sorted. It has a time complexity of O(n^2), making it inefficient for large data sets. The algorithm iterates through the list multiple times, comparing and swapping elements. However, it is easy to understand and implement, making it suitable for small or nearly sorted arrays where simplicity is prioritized over efficiency. For larger data sets, more efficient sorting algorithms like QuickSort or MergeSort are preferred.

July 13, 2023
Linked List Implementation: Swift

A linked list is a data structure in Swift that consists of nodes linked together via pointers or references. Each node contains data and a reference to the next node. Unlike an array, a linked list does not require contiguous memory allocation. Insertions and deletions can be performed efficiently in a linked list by updating the pointers, but accessing elements requires traversing the list linearly. Linked lists are useful when frequent insertions or deletions are expected, and their time complexity for most operations is O(1) or O(n) depending on the operation.

July 11, 2023
Remove duplicates from array: Swift

To remove duplicates from an array in Swift using a dictionary, we can create an empty dictionary and iterate through the array. For each element, we use it as a key in the dictionary and assign a dummy value. The dictionary automatically removes duplicate keys, so we end up with only unique elements. The time complexity of this approach is O(n), where n is the number of elements in the array, making it an efficient solution for removing duplicates..

July 8, 2023
Binary Serach : Swift

Binary search is an efficient search algorithm used to locate a specific element in a sorted array or list. It works by repeatedly dividing the search space in half until the target element is found. In Swift, binary search is typically implemented recursively or iteratively. The time complexity of binary search is O(log n), where n is the number of elements in the array. This makes it highly efficient for large data sets as it eliminates half of the remaining search space at each step, significantly reducing the search time.

July 8, 2023
Merge 2 sorted Array: Swift

Merging two sorted Int arrays in Swift means combining them into a single sorted array while preserving the order. With Swift, you can efficiently implement the merging algorithm. By comparing elements in both arrays and arranging them accordingly, you’ll obtain a new sorted array. This process ensures that the elements from both arrays are in ascending order, allowing you to organize your data effectively. The merged sorted Int array can be readily used in your Swift app for various purposes like displaying sorted lists or conducting efficient searches.

Prev 1 2