This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var a = 1 | |
var b = 2 | |
print("Before swap: \(a) , \(b)") | |
// swapping | |
let temp = a | |
a = b | |
b = temp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int x, y; | |
printf("before swapping numbers: %d %d\n",x,y); | |
/*swapping*/ | |
x = x + y - (y = x) | |
printf("before swapping numbers: %d %d\n",x,y); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x = 5 | |
y = 10 | |
x, y = y, x | |
print("x =", x) | |
print("y =", y) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func swap<T:Comparable>(_ a:inout T, _ b :inout T) { | |
(a,b) = (b,a) | |
} | |
var a = 1 | |
var b = 2 | |
swap(&a,&b) | |
print(a) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BalancedBrackets { | |
func checkBalancedBrackets(string: String) -> Bool { | |
var stack = [Character]() | |
let mapBrackets : [Character: Character] = ["}":"{", ")":"(", "]":"["] | |
let openingBrackets = "([{" | |
for char in string { | |
if openingBrackets.contains(char){ | |
stack.append(char) | |
} | |
else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func findMajorityElement(_ input:[Int]) -> Int{ | |
var frequencyMap = [Int:Int]() | |
for num in input { | |
if let count = frequencyMap[num] { | |
frequencyMap[num] = count + 1 | |
} | |
else { | |
frequencyMap[num] = 1 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using Boyer-Moore voting algorithm | |
func findMajorityElement(_ input:[Int]) -> Int { | |
var maxElement = 0 | |
var count = 0 | |
// Iteration 1 : find max frequency element | |
for num in input { | |
if num == maxElement { | |
count += 1 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var stack = [Character]() | |
let mapBrackets : [Character: Character] = ["}":"{", ")":"(", "]":"["] | |
let openingBrackets = "([{" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for char in string { | |
if openingBrackets.contains(char){ | |
stack.append(char) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if let last = stack.last, last == mapBrackets[char] { | |
stack.popLast() | |
} |
OlderNewer