Skip to content

Commit 689e381

Browse files
committed
Solved leetcode array problems.
1 parent 59ebbe6 commit 689e381

File tree

10 files changed

+152
-0
lines changed

10 files changed

+152
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import UIKit
2+
3+
//MARK: - https://leetcode.com/problems/majority-element/description/?envType=study-plan-v2&envId=top-interview-150
4+
/* Given an array nums of size n, return the majority element.
5+
The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.*/
6+
7+
//Time: O(n) Space: O(1)
8+
9+
func majorityElement(_ nums: [Int]) -> Int {
10+
var majorityNumber = 0, count = 0
11+
nums.forEach { num in
12+
if count == 0 {
13+
majorityNumber = num
14+
}
15+
count += num == majorityNumber ? 1 : -1
16+
}
17+
return majorityNumber
18+
}
19+
20+
var nums = [2,2,1,1,1,2,2]
21+
print("Majority number is \(majorityElement(nums))")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import UIKit
2+
3+
//MARK: - https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/?envType=study-plan-v2&envId=top-interview-150
4+
5+
// - Complexity:
6+
// - time: O(n), where m is the number of elements in nums1, and n is the number of elements in nums2.
7+
// - space: O(1), only constant space is used.
8+
9+
func removeDuplicates(_ nums: inout [Int]) -> Int {
10+
if nums.count == 0 {
11+
return 0
12+
}
13+
var i = 0
14+
for j in 1..<nums.count {
15+
guard nums[i] != nums[j] else { continue }
16+
i += 1
17+
nums[i] = nums[j]
18+
}
19+
return i + 1
20+
}
21+
22+
23+
24+
var num1 = [1,1,1,2,2,3] // [0,0,1,1,1,2,2,3,3,4]
25+
print("Number of unique elements in array is \(removeDuplicates(&num1))")
26+
print(num1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import UIKit
2+
3+
//MARK: - https://leetcode.com/problems/remove-element/description/?envType=study-plan-v2&envId=top-interview-150
4+
// - Complexity:
5+
// - time: O(n), where m is the number of elements in nums1, and n is the number of elements in nums2.
6+
// - space: O(1), only constant space is used.
7+
8+
func removeElement(_ nums: inout [Int], _ val: Int) -> Int {
9+
guard nums.count > 0 else {
10+
return 0
11+
}
12+
var i = nums.count - 1
13+
while i >= 0 {
14+
if nums[i] == val {
15+
nums.remove(at : i)
16+
}
17+
i -= 1
18+
}
19+
return nums.count
20+
}
21+
var nums = [3,2,2,3]
22+
print(removeElement(&nums, 3))
23+
print(nums)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import UIKit
2+
3+
//MARK: - https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/description/?envType=study-plan-v2&envId=top-interview-150
4+
// - Complexity:
5+
// - time: O(n), where m is the number of elements in nums1, and n is the number of elements in nums2.
6+
// - space: O(1), only constant space is used.
7+
8+
func removeDuplicates(_ nums: inout [Int]) -> Int {
9+
let length = nums.count
10+
guard length > 2 else {
11+
return length
12+
}
13+
var currentIndex = 1
14+
for nextIndex in 2..<length where nums[currentIndex] != nums[currentIndex - 1] || nums[currentIndex] != nums[nextIndex] {
15+
currentIndex += 1
16+
nums[currentIndex] = nums[nextIndex]
17+
}
18+
return currentIndex + 1
19+
}
20+
21+
var num1 = [1,1,1,2,2,3]
22+
print("Number of elements in array after removing third common element is \(removeDuplicates(&num1))")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import UIKit
2+
3+
//MARK: - https://leetcode.com/problems/merge-sorted-array/description/?envType=study-plan-v2&envId=top-interview-150
4+
// - Complexity:
5+
// - time: O(m + n), where m is the number of elements in nums1, and n is the number of elements in nums2.
6+
// - space: O(1), only constant space is used.
7+
8+
func merge(_ nums1: inout [Int], _ m: Int, _ nums2: [Int], _ n: Int) {
9+
var m = m - 1
10+
var n = n - 1
11+
var last = m + n + 1
12+
// Compare elements from the end of both arrays and insert the larger one at the end of nums1
13+
while m >= 0 && n >= 0 {
14+
if nums1[m] > nums2[n] {
15+
nums1[last] = nums1[m]
16+
m -= 1
17+
} else {
18+
nums1[last] = nums2[n]
19+
n -= 1
20+
}
21+
last -= 1
22+
}
23+
24+
// If there are remaining elements in nums2, insert them in nums1
25+
while n >= 0 {
26+
nums1[last] = nums2[n]
27+
last -= 1
28+
n -= 1
29+
}
30+
}
31+
32+
var nums1 = [1,2,3,0,0,0]
33+
var nums2 = [2,5,6]
34+
print("nums1 before merging :- \(nums1)")
35+
print("---MERGING ELEMENTs---")
36+
merge(&nums1, 3, nums2, 3)
37+
nums1.remove(at: <#T##Int#>)
38+
print("nums1 after merging :- \(nums1)")
39+
40+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

0 commit comments

Comments
 (0)