Bibi's DevLog ๐Ÿค“๐ŸŽ

[Swift] removeFirst(), removeLast(), removeFirst(_:), removeLast(_:) ๋ณธ๋ฌธ

๐Ÿ“ฑ๐ŸŽ iOS/๐Ÿ•Š Swift

[Swift] removeFirst(), removeLast(), removeFirst(_:), removeLast(_:)

๋น„๋น„ bibi 2022. 10. 6. 15:45

https://developer.apple.com/documentation/swift/array/removefirst()

https://developer.apple.com/documentation/swift/array/removelast()

removeFirst() : ๋งจ ์•ž์˜ ์š”์†Œ ์ œ๊ฑฐ. O(n)

removeLast() : ๋งจ ๋’ค์˜ ์š”์†Œ ์ œ๊ฑฐ. O(1)

  • ๋‘˜ ๋‹ค ๋นˆ ๋ฐฐ์—ด์—๋Š” ์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅ
  • removeFirst() ์˜ ๊ฒฝ์šฐ O(n) ์ด๋ฏ€๋กœ ํ์˜ dequeue๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

https://developer.apple.com/documentation/swift/array/removefirst(_:)

https://developer.apple.com/documentation/swift/array/removelast(_:)

removeFirst(_ k: Int) : ๋งจ ์•ž์—์„œ๋ถ€ํ„ฐ k๊ฐœ์˜ ์š”์†Œ ์ œ๊ฑฐ. O(n)

removeLast(_ k: Int) : ๋งจ ๋’ค์—์„œ๋ถ€ํ„ฐ k๊ฐœ์˜ ์š”์†Œ ์ œ๊ฑฐ. O(k)

  • k๊ฐ€ ๋ฐฐ์—ด ํฌ๊ธฐ๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ ์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅ
  • ๋ฐฐ์—ด์—์„œ k๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ, k๊ฐœ์˜ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ์ž„์— ์œ ์˜
var array = [1, 2, 3]
array.removeFirst(2)
print(array)
// [1, 3] ์ด ์•„๋‹Œ, [3]์ด ๊ฒฐ๊ณผ๊ฐ€ ๋œ๋‹ค.