From 019f20a2c714ed05f17a7e174ba9da0cc8b820e9 Mon Sep 17 00:00:00 2001 From: cediackermann Date: Mon, 8 Jun 2026 10:39:44 +0200 Subject: [PATCH] make purely pure functions --- Sources/LeetCode/LeetCode.swift | 66 ++++++++++++++------------------- Sources/LeetCode/main.swift | 10 ++--- 2 files changed, 31 insertions(+), 45 deletions(-) diff --git a/Sources/LeetCode/LeetCode.swift b/Sources/LeetCode/LeetCode.swift index 14e7a36..c4667cb 100644 --- a/Sources/LeetCode/LeetCode.swift +++ b/Sources/LeetCode/LeetCode.swift @@ -1,50 +1,38 @@ import Foundation -// -// LeetCode.swift -// m323 -// -// Created by cediackermann on 18.05.2026. -// - -class LeetCode { - func hammingWeight(_ n: Int) -> Int { - return n == 0 ? 0 : (n & 1) + hammingWeight(n >> 1) +enum LeetCode { + static func hammingWeight(_ n: Int) -> Int { + n == 0 ? 0 : (n & 1) + hammingWeight(n >> 1) } - func singleNumber(_ nums: [Int]) -> Int { - var result = 0 - for num in nums { - result ^= num + static func singleNumber(_ nums: [Int]) -> Int { + nums.reduce(0, ^) + } + + static func containsDuplicate(_ nums: [Int]) -> Bool { + nums.count != Set(nums).count + } + + static func sortedSquares(_ nums: [Int]) -> [Int] { + nums.map { $0 * $0 }.sorted() + } + + static func productExceptSelf(_ nums: [Int]) -> [Int] { + let n = nums.count + var result = [Int](repeating: 1, count: n) + + var prefix = 1 + for i in 0.. Bool { - return nums.count != Set(nums).count - } - - func sortedSquares(_ nums: [Int]) -> [Int] { - return nums.map { $0 * $0 }.sorted() - } - - func productExceptSelf(_ nums: [Int]) -> [Int] { - var result = [Int](repeating: 1, count: nums.count) - var prefix = [Int](repeating: 1, count: nums.count) - var suffix = [Int](repeating: 1, count: nums.count) - var runningProduct = 1 - for i in 0..