Skip to content

Commit

Permalink
feat(arrays): intersection of arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianLusina committed Jun 18, 2024
1 parent a68c11a commit 350f603
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.kotlinground.datastructures.arrays.intersectionofarrays
package com.kotlinground.algorithms.arrays.intersection

fun setIntersection(setOne: Set<Int>, setTwo: Set<Int>): IntArray {
return setOne.intersect(setTwo).toIntArray()
fun <T> setIntersection(setOne: Set<T>, setTwo: Set<T>): List<T> {
return setOne.intersect(setTwo).toList()
}

/**
Expand All @@ -10,9 +10,9 @@ fun setIntersection(setOne: Set<Int>, setTwo: Set<Int>): 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 <T> intersectionOne(listOne: Array<T>, listTwo: Array<T>): List<T> {
val setOne = listOne.toSet()
val setTwo = listTwo.toSet()

return if (setOne.size < setTwo.size) {
setIntersection(setOne, setTwo)
Expand All @@ -21,18 +21,18 @@ fun intersectionOne(nums1: IntArray, nums2: IntArray): IntArray {
}
}

fun intersectionTwo(nums1: IntArray, nums2: IntArray): IntArray {
val counter = hashMapOf<Int, Int>()
val result = arrayListOf<Int>()
fun <T> intersectionTwo(listOne: Array<T>, listTwo: Array<T>): List<T> {
val counter = hashMapOf<T, Int>()
val result = arrayListOf<T>()

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
}
Original file line number Diff line number Diff line change
@@ -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))
}

}
Original file line number Diff line number Diff line change
@@ -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))
}

}

This file was deleted.

This file was deleted.

0 comments on commit 350f603

Please sign in to comment.