diff --git a/datastructures/src/main/kotlin/com/kotlinground/datastructures/arrays/intersectionofarrays/README.md b/algorithms/src/main/kotlin/com/kotlinground/algorithms/arrays/intersection/README.md similarity index 100% rename from datastructures/src/main/kotlin/com/kotlinground/datastructures/arrays/intersectionofarrays/README.md rename to algorithms/src/main/kotlin/com/kotlinground/algorithms/arrays/intersection/README.md diff --git a/datastructures/src/main/kotlin/com/kotlinground/datastructures/arrays/intersectionofarrays/intersectionOfArrays.kt b/algorithms/src/main/kotlin/com/kotlinground/algorithms/arrays/intersection/intersection.kt similarity index 55% rename from datastructures/src/main/kotlin/com/kotlinground/datastructures/arrays/intersectionofarrays/intersectionOfArrays.kt rename to algorithms/src/main/kotlin/com/kotlinground/algorithms/arrays/intersection/intersection.kt index 40ee47a1..b414a597 100644 --- a/datastructures/src/main/kotlin/com/kotlinground/datastructures/arrays/intersectionofarrays/intersectionOfArrays.kt +++ b/algorithms/src/main/kotlin/com/kotlinground/algorithms/arrays/intersection/intersection.kt @@ -1,7 +1,7 @@ -package com.kotlinground.datastructures.arrays.intersectionofarrays +package com.kotlinground.algorithms.arrays.intersection -fun setIntersection(setOne: Set, setTwo: Set): IntArray { - return setOne.intersect(setTwo).toIntArray() +fun setIntersection(setOne: Set, setTwo: Set): List { + return setOne.intersect(setTwo).toList() } /** @@ -10,9 +10,9 @@ fun setIntersection(setOne: Set, setTwo: Set): IntArray { * checking the presence of each element in the other larger set. The time complexity is O(n+m) where n is the size of * the first array and m is the size of the second array. */ -fun intersectionOne(nums1: IntArray, nums2: IntArray): IntArray { - val setOne = nums1.toSet() - val setTwo = nums2.toSet() +fun intersectionOne(listOne: Array, listTwo: Array): List { + val setOne = listOne.toSet() + val setTwo = listTwo.toSet() return if (setOne.size < setTwo.size) { setIntersection(setOne, setTwo) @@ -21,18 +21,18 @@ fun intersectionOne(nums1: IntArray, nums2: IntArray): IntArray { } } -fun intersectionTwo(nums1: IntArray, nums2: IntArray): IntArray { - val counter = hashMapOf() - val result = arrayListOf() +fun intersectionTwo(listOne: Array, listTwo: Array): List { + val counter = hashMapOf() + val result = arrayListOf() - nums1.forEach { counter[it] = counter.getOrDefault(it, 0) + 1 } + listOne.forEach { counter[it] = counter.getOrDefault(it, 0) + 1 } - nums2.forEach { + listTwo.forEach { if (counter.containsKey(it) && counter[it]!! > 0) { result.add(it) counter[it] = counter[it]!! - 1 } } - return result.toIntArray() + return result } diff --git a/algorithms/src/test/kotlin/com/kotlinground/algorithms/arrays/intersection/IntersectionOfArraysTest.kt b/algorithms/src/test/kotlin/com/kotlinground/algorithms/arrays/intersection/IntersectionOfArraysTest.kt new file mode 100644 index 00000000..f252e2bd --- /dev/null +++ b/algorithms/src/test/kotlin/com/kotlinground/algorithms/arrays/intersection/IntersectionOfArraysTest.kt @@ -0,0 +1,24 @@ +package com.kotlinground.algorithms.arrays.intersection + +import org.junit.jupiter.api.Test +import kotlin.test.assertContentEquals + +class IntersectionOfArraysTest { + + @Test + fun `Should return (2) for nums1=(1,2,2,1) and nums2=(2,2)`() { + val nums1 = arrayOf(1, 2, 2, 1) + val nums2 = arrayOf(2, 2) + val expected = listOf(2) + assertContentEquals(expected, intersectionOne(nums1, nums2)) + } + + @Test + fun `Should return (9,4) for nums1=(4,9,5) and nums2=(9,4,9,8,4)`() { + val nums1 = arrayOf(4, 9, 5) + val nums2 = arrayOf(9, 4, 9, 8, 4) + val expected = listOf(9, 4) + assertContentEquals(expected, intersectionOne(nums1, nums2)) + } + +} \ No newline at end of file diff --git a/algorithms/src/test/kotlin/com/kotlinground/algorithms/arrays/intersection/IntersectionTwoOfArraysTest.kt b/algorithms/src/test/kotlin/com/kotlinground/algorithms/arrays/intersection/IntersectionTwoOfArraysTest.kt new file mode 100644 index 00000000..dee3a721 --- /dev/null +++ b/algorithms/src/test/kotlin/com/kotlinground/algorithms/arrays/intersection/IntersectionTwoOfArraysTest.kt @@ -0,0 +1,24 @@ +package com.kotlinground.algorithms.arrays.intersection + +import org.junit.jupiter.api.Test +import kotlin.test.assertContentEquals + +class IntersectionTwoOfArraysTest { + + @Test + fun `Should return (2,2) for nums1=(1,2,2,1) and nums2=(2,2)`() { + val nums1 = arrayOf(1, 2, 2, 1) + val nums2 = arrayOf(2, 2) + val expected = listOf(2, 2) + assertContentEquals(expected, intersectionTwo(nums1, nums2)) + } + + @Test + fun `Should return (9,4) or (4, 9) for nums1=(4,9,5) and nums2=(9,4,9,8,4)`() { + val nums1 = arrayOf(4, 9, 5) + val nums2 = arrayOf(9, 4, 9, 8, 4) + val expected = listOf(9, 4) + assertContentEquals(expected, intersectionTwo(nums1, nums2)) + } + +} \ No newline at end of file diff --git a/datastructures/src/test/kotlin/com/kotlinground/datastructures/arrays/intersectionofarrays/IntersectionOfArraysTest.kt b/datastructures/src/test/kotlin/com/kotlinground/datastructures/arrays/intersectionofarrays/IntersectionOfArraysTest.kt deleted file mode 100644 index 95de82b8..00000000 --- a/datastructures/src/test/kotlin/com/kotlinground/datastructures/arrays/intersectionofarrays/IntersectionOfArraysTest.kt +++ /dev/null @@ -1,24 +0,0 @@ -package com.kotlinground.datastructures.arrays.intersectionofarrays - -import org.junit.jupiter.api.Assertions.assertArrayEquals -import org.junit.jupiter.api.Test - -class IntersectionOfArraysTest { - - @Test - fun `Should return (2) for nums1=(1,2,2,1) and nums2=(2,2)`() { - val nums1 = intArrayOf(1, 2, 2, 1) - val nums2 = intArrayOf(2, 2) - val expected = intArrayOf(2) - assertArrayEquals(expected, intersectionOne(nums1, nums2)) - } - - @Test - fun `Should return (9,4) for nums1=(4,9,5) and nums2=(9,4,9,8,4)`() { - val nums1 = intArrayOf(4, 9, 5) - val nums2 = intArrayOf(9, 4, 9, 8, 4) - val expected = intArrayOf(9, 4) - assertArrayEquals(expected, intersectionOne(nums1, nums2)) - } - -} \ No newline at end of file diff --git a/datastructures/src/test/kotlin/com/kotlinground/datastructures/arrays/intersectionofarrays/IntersectionTwoOfArraysTest.kt b/datastructures/src/test/kotlin/com/kotlinground/datastructures/arrays/intersectionofarrays/IntersectionTwoOfArraysTest.kt deleted file mode 100644 index 0ce4e792..00000000 --- a/datastructures/src/test/kotlin/com/kotlinground/datastructures/arrays/intersectionofarrays/IntersectionTwoOfArraysTest.kt +++ /dev/null @@ -1,24 +0,0 @@ -package com.kotlinground.datastructures.arrays.intersectionofarrays - -import org.junit.jupiter.api.Assertions.assertArrayEquals -import org.junit.jupiter.api.Test - -class IntersectionTwoOfArraysTest { - - @Test - fun `Should return (2,2) for nums1=(1,2,2,1) and nums2=(2,2)`() { - val nums1 = intArrayOf(1, 2, 2, 1) - val nums2 = intArrayOf(2, 2) - val expected = intArrayOf(2, 2) - assertArrayEquals(expected, intersectionTwo(nums1, nums2)) - } - - @Test - fun `Should return (9,4) or (4, 9) for nums1=(4,9,5) and nums2=(9,4,9,8,4)`() { - val nums1 = intArrayOf(4, 9, 5) - val nums2 = intArrayOf(9, 4, 9, 8, 4) - val expected = intArrayOf(9, 4) - assertArrayEquals(expected, intersectionTwo(nums1, nums2)) - } - -} \ No newline at end of file