Define a method/function that removes from a given array of integers all the values contained in a second array.
Examples:
int[] integerList = new int[]{ 1, 2, 2, 3, 1, 2, 3, 4 }
int[] valuesList = new int[]{ 1, 3 }
Kata.Remove(integerList, valuesList) == new int[]{ 2, 2, 4 } // --> true
int[] integerList = new int[]{ 1, 1, 2, 3, 1, 2, 3, 4, 4, 3 ,5, 6, 7, 2, 8 }
int[] valuesList = new int[]{ 1, 3, 4, 2 }
Kata.Remove(integerList, valuesList) == new int[]{ 5, 6 ,7 ,8 } // --> true
int[] integerList = new int[]{ 8, 2, 7, 2, 3, 4, 6, 5, 4, 4, 1, 2 , 3 }
int[] valuesList = new int[]{ 2, 4, 3 }
Kata.Remove(integerList, valuesList) == new int[]{ 8, 7, 6, 5, 1 } // --> true
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Kata
{
public static int[] Remove(int[] integerList,int[] valuesList)
{
if (integerList==null||integerList.length<1) return new int[0];
if (valuesList==null||valuesList.length<1) return integerList;
List<Integer> list= Arrays.stream(valuesList).boxed().collect(Collectors.toList());
return Arrays.stream(integerList).filter(i->!list.contains(i)).toArray();
}
}
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runners.JUnit4;
import java.util.Arrays;
// TODO: Replace examples and use TDD development by writing your own tests
public class SolutionTest {
@Test
public void test1()
{
int[] integerList = new int[]{ 1, 1, 2, 3, 1, 2, 3, 4 };
int[] valuesList = new int[]{ 1, 3 };
assertTrue(Arrays.equals(new int[]{ 2, 2, 4 },Kata.Remove(integerList,valuesList)));
}
@Test
public void test2()
{
int[] integerList = new int[]{1, 1, 2, 3, 1, 2 ,3 , 4, 4, 3 ,5, 6, 7, 2, 8 };
int[] valuesList = new int[]{1, 3, 4, 2 };
assertTrue(Arrays.equals(new int[]{ 5, 6 ,7 ,8 },Kata.Remove(integerList,valuesList)));
}
@Test
public void test3()
{
int[] integerList = new int[]{8, 2, 7, 2, 3, 4, 6, 5, 4, 4, 1, 2 , 3 };
int[] valuesList = new int[]{2, 4, 3 };
assertTrue(Arrays.equals(new int[]{ 8, 7, 6, 5, 1 },Kata.Remove(integerList,valuesList)));
}
@Test
public void nullTest1()
{
int[] integerList = null;
int[] valuesList = new int[]{2, 4, 3 };
assertTrue(Arrays.equals(new int[]{},Kata.Remove(integerList,valuesList)));
}
@Test
public void nullTest2()
{
int[] integerList = new int[]{8, 2, 7, 2, 3, 4, 6, 5, 4, 4, 1, 2 , 3 };
int[] valuesList = null;
assertTrue(Arrays.equals(new int[]{8, 2, 7, 2, 3, 4, 6, 5, 4, 4, 1, 2 , 3 },Kata.Remove(integerList,valuesList)));
}
}