TutorialEdge

Challenge 04 - Filtering Duplicates

Welcome, everyone to the discussion thread for the 4th Go challenge posted to the site :tada:

Feel free to show us how you solved the challenge by posting your solutions below!

Solution:

package main

import "fmt"

type Developer struct {
	Name string
	Age  int
}

func FilterUnique(developers []Developer) []string {
	uniqueMap := make(map[string]bool)
	var uniqueNames []string

	for _, dev := range developers {
		if _, exists := uniqueMap[dev.Name]; !exists {
			uniqueMap[dev.Name] = true
			uniqueNames = append(uniqueNames, dev.Name)
		}
	}
	return uniqueNames
}

func main() {
	fmt.Println("Filter Unique Challenge")
}

There is a mistake in the example output. Its missing Paul as unique name

It should be:

// output
[]string{
  "Elliot",
  "Alan",
  "Jennifer",
  "Graham",
  "Paul",
}

Another good catch! :pray: I’ve updated the text in the challenge and the fix should be going live!

func FilterUnique(developers []Developer) []string {
	var names []string
	for _, v := range developers {
		same := false
		for _, n := range names {
			if n == v.Name {
				same = true
			}
		}
		if !same {
			names = append(names, v.Name)
		}
	}
	return names
}

PROBABLY THE EASIEST IMPLEMENTATION

func FilterUnique(developers []Developer) []string {
  var uniques []string
  check:=make(map[string]int)
  for _,developer:=range developers{
  	check[developer.Name]=1
  }
  
  for name,_ :=range check{
    uniques=append(uniques,name)
  }
  return uniques
}