forked from aditya109/git-osp-for-beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Remove Duplicates in Kotlin (aditya109#534)
Signed-off-by: nitishanand99 <nitish.anand99@gmail.com>
- Loading branch information
1 parent
3cd8fad
commit c062be8
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |