Skip to content

Instantly share code, notes, and snippets.

@tayadehritik
Created September 7, 2024 16:47
Show Gist options
  • Select an option

  • Save tayadehritik/890f561f3f1a2d09ed37b29300d9f33f to your computer and use it in GitHub Desktop.

Select an option

Save tayadehritik/890f561f3f1a2d09ed37b29300d9f33f to your computer and use it in GitHub Desktop.
class Solution {
fun twoSum(nums: IntArray, target: Int): IntArray {
val myMap = mutableMapOf<Int,Int>()
for(i in 0..nums.size-1) {
val difference = target - nums[i]
val differenceIndex = myMap.getOrElse(difference){-1}
if(differenceIndex != -1) {
return intArrayOf(i,differenceIndex)
}
else{
myMap[nums[i]] = i
}
}
return intArrayOf(0,0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment