diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index 1e277c1..6f1119f 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -30,4 +30,11 @@ fun main() { node3.next = node1 // cycle back to x val llcdResult = linkedListCycleDetection.hasCycle(node1) println("Has cycle $llcdResult") + + // Calling Contains Duplicate + val `containsDuplicate.kt` = `ContainsDuplicate.kt`() + val containsDuplicateResult = `containsDuplicate.kt` + .containsDuplicate(intArrayOf(1,2,3,1,0,1)) + println("Contains Duplicate Integers $containsDuplicateResult") + } \ No newline at end of file diff --git a/src/main/kotlin/dsalgoleetcode/ContainsDuplicate.kt.kt b/src/main/kotlin/dsalgoleetcode/ContainsDuplicate.kt.kt new file mode 100644 index 0000000..b20745c --- /dev/null +++ b/src/main/kotlin/dsalgoleetcode/ContainsDuplicate.kt.kt @@ -0,0 +1,27 @@ +package main.kotlin.dsalgoleetcode + +/** + * Contains Duplicate + * Given an integer array nums, return true if any value appears at least twice in the + * array, and return false if every element is distinct. + * + * Example 1: + * Input: nums = [1,2,3,1] + * Output: true + * Explanation: + * The element 1 occurs at the indices 0 and 3. + * */ + +class `ContainsDuplicate.kt` { + + fun containsDuplicate(nums: IntArray): Boolean { + + val seen = mutableSetOf() + for(i in nums){ + if( i in seen) + return true + seen.add(i) + } + return false + } +} \ No newline at end of file