-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove duplicates and return unique nos in array
- Loading branch information
Nishi Davidson
authored and
Nishi Davidson
committed
Mar 27, 2020
1 parent
00261db
commit a9a91cc
Showing
1 changed file
with
36 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,36 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
|
||
nums := []int{0,0,1,1,1,2,3,4,4} | ||
fmt.Println(removeDuplicates(nums)) | ||
|
||
} | ||
|
||
func removeDuplicates(nums []int) int { | ||
m := make(map[int]int) | ||
|
||
for i:=0; i<len(nums); i++ { | ||
_, ok := m[nums[i]] | ||
if ok == false { | ||
m[nums[i]] = 1 | ||
} else { | ||
if i != len(nums)-1 { | ||
nums = append(nums[:i], nums[i+1:]...) | ||
i = i-1 | ||
} else { | ||
nums = nums[:i] | ||
break | ||
} | ||
} | ||
} | ||
|
||
fmt.Println(m) | ||
fmt.Println(nums) | ||
|
||
return len(nums) | ||
} |