Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 32b8015

Browse files
committed
Added ProductofArrayExceptSelf
1 parent ea950e4 commit 32b8015

File tree

1 file changed

+26
-1
lines changed
  • CodingInterview/CodingInterview

1 file changed

+26
-1
lines changed

‎CodingInterview/CodingInterview/Tests.cs‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,21 @@ int GetTrappingRainWater(int[] elevationMap)
8585
return waterToDrop;
8686
}
8787

88-
[Test]
88+
int [] GetProductofArrayExceptSelf(int [] inputArray)
89+
{
90+
if (inputArray == null || inputArray.Length == 0)
91+
throw new ArgumentNullException("inputArray needs to have elements");
92+
var outputArray = new List<int>();
93+
for (int index = 0; index < inputArray.Length; index++)
94+
{
95+
outputArray.Add(inputArray
96+
.Where(element => element != inputArray[index])
97+
.Aggregate((firstElement, secondElement) => firstElement * secondElement));
98+
}
99+
return outputArray.ToArray();
100+
}
101+
102+
[Test]
89103
public void RemoveDuplicatesFromSortedArray_1()
90104
{
91105
var sortedArrayWithDuplicates = new int[] { 1, 3, 3, 6, 8, 8, 9 };
@@ -140,5 +154,16 @@ public void TrappingRainWater_6()
140154

141155
Assert.AreEqual(expectedValue, rainWaterValue);
142156
}
157+
158+
[Test]
159+
public void ProductofArrayExceptSelf_7()
160+
{
161+
var inputArray = new int[] { 1, 2, 3, 4};
162+
var expectedArray = new int[] { 24, 12, 8, 6 };
163+
164+
var outputArray = GetProductofArrayExceptSelf(inputArray);
165+
166+
Assert.AreEqual(expectedArray, outputArray);
167+
}
143168
}
144169
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /