Go program to remove all duplicate entries from a slice

Milind Dhoke
2 min readAug 8, 2019

--

Well, I was working on a go program which is able to remove all duplicate email id’s collected in a log file. While doing so I thought to publish a blog so that I can save some one’s time who is looking out a similar solution on the web.
The program that I coded here is responsible for removing all duplicate email id’s from a log file. Instead of writing much let's move ahead.

Problem Statement:

Consider an application is writing a log file. This log file contains the data like id, firstName, lastName, and email id. Ideally, all duplicate email fields should not be allowed, for that purpose, we should have a program which can find all unique ids for further processing and display all duplicate email id’s.

Consider below log file which is an input to our program. Name of the file is emailsorterdata.log

Solution:

As you can see 20 emails have been registered containing duplicate email id’s.

Go Code

Output:

mdhoke@codingFun[master] ➤ go run emailSorter.goNumber of duplicate id's is --> 5 and those are are --> [pgil@gmail.com bc@outlook.co kc@yahoo.co mhost@outlook.co.in mild@hotmail.com cpil@gmail.edu hs@gmail.com apant@yahoo.com pmich@yahoo.com glanc@google.io ebull@yahoo.com st@gmail.com johndoe@gmail.com johny.bog@gmail.com ljeans@gmail.com]Number od uniques email id's is --> 15 and those are are --> [pmich@yahoo.com kc@yahoo.co johndoe@gmail.com mild@hotmail.com pgil@gmail.com]

You can find this program here @ Github
Link:
Go program: https://github.com/dmilind/golang/blob/master/codingFun/emailSorter.go
Log File: https://github.com/dmilind/golang/blob/master/codingFun/emailsorterdata.log

--

--