Skip to content
This repository has been archived by the owner on Oct 14, 2021. It is now read-only.

Commit

Permalink
Added Remove Duplicates in Kotlin (#534) (#548)
Browse files Browse the repository at this point in the history
Signed-off-by: nitishanand99 <nitish.anand99@gmail.com>
  • Loading branch information
nitishanand99 authored Oct 14, 2021
1 parent 9ac7636 commit abc5b3b
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Programming/Kotlin/RemoveDuplicates/RemoveDuplicates.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.example.testapp

import java.util.*
import kotlin.collections.ArrayList

fun main(){
val scanner = Scanner(System.`in`)
println("Enter the size of array")
val size = scanner.nextInt()

val nums = arrayListOf<Int>()
println("Enter the $size numbers")
for(i in 0 until size) nums.add(scanner.nextInt())

val k = removeDuplicates(nums)

println("Size of the new array is $k")
print("The new array is:\n[")
for(num in nums){
if (num==nums[nums.size-1]) print("$num]")
else print("$num,")
}

}

fun removeDuplicates(nums : ArrayList<Int>): Int{
var size = nums.size
var i = 0
while (i<size){
innerLoop@
for(j in i+1 until size){
if (nums[i] == nums[j]){
nums.removeAt(i)
size = nums.size
i--
break@innerLoop
}
}
i++
}
return nums.size
}

0 comments on commit abc5b3b

Please sign in to comment.