Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8930d12

Browse files
Merge pull request #4 from sumanas27/ss-contains-duplicate
Find duplicate elements in an array
2 parents 661ee70 + 93499b2 commit 8930d12

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

‎src/main/kotlin/Main.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,11 @@ fun main() {
3030
node3.next = node1 // cycle back to x
3131
val llcdResult = linkedListCycleDetection.hasCycle(node1)
3232
println("Has cycle $llcdResult")
33+
34+
// Calling Contains Duplicate
35+
val `containsDuplicate.kt` = `ContainsDuplicate.kt`()
36+
val containsDuplicateResult = `containsDuplicate.kt`
37+
.containsDuplicate(intArrayOf(1,2,3,1,0,1))
38+
println("Contains Duplicate Integers $containsDuplicateResult")
39+
3340
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main.kotlin.dsalgoleetcode
2+
3+
/**
4+
* Contains Duplicate
5+
* Given an integer array nums, return true if any value appears at least twice in the
6+
* array, and return false if every element is distinct.
7+
*
8+
* Example 1:
9+
* Input: nums = [1,2,3,1]
10+
* Output: true
11+
* Explanation:
12+
* The element 1 occurs at the indices 0 and 3.
13+
* */
14+
15+
class `ContainsDuplicate.kt` {
16+
17+
fun containsDuplicate(nums: IntArray): Boolean {
18+
19+
val seen = mutableSetOf<Int>()
20+
for(i in nums){
21+
if( i in seen)
22+
return true
23+
seen.add(i)
24+
}
25+
return false
26+
}
27+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /