Mastering Unique Elements and Anagram Detection with Go Maps

Introduction

Welcome to our focused exploration of Go's maps and their valuable applications in solving algorithmic challenges. Building upon the foundation laid in the first unit, this lesson will delve into how these efficient data structures can be leveraged to address and solve various types of problems commonly encountered in technical interviews.

Problem 1: Unique Echo

Picture this: you're given a vast list of words, and you must identify the final word that stands proudly solitary — the last word that is not repeated. Imagine sorting through a database of repeated identifiers and finding one identifier towards the end of the list that is unlike any other.

Naive Approach

The straightforward approach would be to examine each word in reverse, comparing it to every other word for uniqueness. This brute-force method would result in poor time complexity, O(n2)O(n^2), which is less than ideal for large datasets.

Here is the naive approach in Go:

Go
func FindLastUniqueWordNaive(words []string) string {
    // Traverse the list from the end
    for i := len(words) - 1; i >= 0; i-- {
        isUnique := true
        // Compare the current word to all other words
        for j := 0; j < len(words); j++ {
            // If a duplicate is found, mark as not unique
            if i != j && words[i] == words[j] {
                isUnique = false
                break
            }
        }

        // If the word is unique, return it
        if isUnique {
            return words[i]
        }
    }

    // If no unique word is found, return an empty string
    return ""
}

Efficient Approach

We can utilize two maps: wordsMap to maintain the count of each word and duplicatesMap to keep track of duplicate words. By the end, we can remove all duplicated words to achieve our goal. Here's how to solve the problem using Go's map:

Go
func FindLastUniqueWordEfficient(words []string) string {
    // Initialize a map to store the count of each word
    wordsMap := make(map[string]int)

    // Traverse the list to populate word counts
    for _, word := range words {
        wordsMap[word]++
    }

    // Iterate from the end to find the last unique word
    for i := len(words) - 1; i >= 0; i-- {
        if wordsMap[words[i]] == 1 {
            return words[i] // Return the last unique word
        }
    }
    return "" // If no unique word is found
}

Explanation:

  • We first initialize a map called wordsMap to keep track of the frequency of each word within the list.
  • We then iterate over the words list using a loop to populate wordsMap, incrementing the count for each occurrence of a word.
  • To find the last unique word, we again loop through the list, but from the end this time. We check wordsMap to see if the word appears exactly once (i.e., has a count of 1). If it is unique, we return it immediately.

The time complexity of the FindLastUniqueWordEfficient function is O(n)O(n), where n is the number of words in the input slice.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal