From 9def407ddb44fd046d60e18f41a8b1a6e0a60f92 Mon Sep 17 00:00:00 2001 From: Hossam Date: Wed, 22 Oct 2025 23:56:58 +0300 Subject: [PATCH] Apply Operations to an Array --- .../Easy/183-Apply Operations to an Array.cs | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 LeetCode C#/Easy/183-Apply Operations to an Array.cs diff --git a/LeetCode C#/Easy/183-Apply Operations to an Array.cs b/LeetCode C#/Easy/183-Apply Operations to an Array.cs new file mode 100644 index 0000000..b970934 --- /dev/null +++ b/LeetCode C#/Easy/183-Apply Operations to an Array.cs @@ -0,0 +1,38 @@ +Link: https://leetcode.com/problems/apply-operations-to-an-array/ +Language: C# + + + + + + + +public class Solution { + public int[] ApplyOperations(int[] nums) + { + for (int i = 0; i < nums.Length - 1; i++) + { + if (nums[i] == nums[i + 1]) + { + nums[i] *= 2; + nums[i + 1] = 0; + } + } + + int index = 0; + + for (int i = 0; i < nums.Length; i++) + { + if (nums[i] != 0) + nums[index++] = nums[i]; + } + + for (int i = index; i < nums.Length; i++) + { + nums[index] = 0; + index++; + } + + return nums; + } +} \ No newline at end of file