Nick Satinover преди 6 години
родител
ревизия
b59dafac98
променени са 2 файла, в които са добавени 57 реда и са изтрити 0 реда
  1. 20
    0
      arraz/src/main/java/Arraz.java
  2. 37
    0
      arraz/src/test/java/ArrazTest.java

+ 20
- 0
arraz/src/main/java/Arraz.java Целия файл

@@ -149,4 +149,24 @@ public class Arraz {
149 149
         maxMin.max = maxVal;
150 150
         return maxMin;
151 151
     }
152
+
153
+    public int[] removeDupesFromArray(int[] ints){
154
+        ArrayList<Integer> retArr = new ArrayList();
155
+        retArr.add(ints[0]);
156
+
157
+        for (int i = 1; i < ints.length; i++) {
158
+            if (!retArr.contains(ints[i])){
159
+                retArr.add(ints[i]);
160
+            }
161
+        }
162
+        int[] intsToRet = new int[retArr.size()];
163
+
164
+        int counter = 0;
165
+        for (int i:retArr) {
166
+            intsToRet[counter] = i;
167
+            counter++;
168
+        }
169
+        return intsToRet;
170
+    }
171
+
152 172
 }

+ 37
- 0
arraz/src/test/java/ArrazTest.java Целия файл

@@ -345,4 +345,41 @@ public class ArrazTest {
345 345
         Assert.assertEquals(expectedMax, actualMax);
346 346
         Assert.assertEquals(expectedMin, actualMin);
347 347
     }
348
+
349
+    @Test
350
+    public void test1removeDupesFromArray() {
351
+        //GIVEN
352
+        int[] arr = {1, 2, 3, 4, 5, 3};
353
+        //WHEN
354
+        int expectedLength = 5;
355
+
356
+        int actualLength = arraz.removeDupesFromArray(arr).length;
357
+        //THEN
358
+        Assert.assertEquals(expectedLength, actualLength);
359
+    }
360
+
361
+    @Test
362
+    public void test2removeDupesFromArray() {
363
+        //GIVEN
364
+        int[] arr = {1, 2, 3, 4, 5, 3, 1, 1, 1, 2, 3, 4, 5};
365
+        //WHEN
366
+        int expectedLength = 5;
367
+
368
+        int actualLength = arraz.removeDupesFromArray(arr).length;
369
+        //THEN
370
+        Assert.assertEquals(expectedLength, actualLength);
371
+    }
372
+
373
+    @Test
374
+    public void test3removeDupesFromArray() {
375
+        //GIVEN
376
+        int[] arr = {1, 2, 3, 4, 5, 3, 1, 1, 1, 2, 3, 4, 5, 6};
377
+        //WHEN
378
+        int expected = 6;
379
+
380
+        int[] actualArray = arraz.removeDupesFromArray(arr);
381
+        int actual = actualArray[5];
382
+        //THEN
383
+        Assert.assertEquals(expected, actual);
384
+    }
348 385
 }