First Unique Character in a String

First Unique Character in a String

We want to find the index of the first character in a string that does not later repeat itself.

We break out a frequency map, an empty object that we will use to store the characters of a string we loop through. If the character is already in the frequency map, we add it to a count. If this is the character's first appearance, we set its' count to 1.

Once everything has been counted and sorted, we can run the original string through a loop, and at each index, we will compare how many times this index of the string, aka character, appears in the frequency map. We are looking for it to match the number one. If it does, we return this index. If it never matches, we return a negative one.

LEO T.

Software Engineer | CompTIA Security+ Certified | 3x AWS Certified (Practitioner, Developer, and Solutions Architect)

1 年

DUDE this is so insightful, thank you for posting! Really helpful way of breaking it down.

Sierra McAliney

Client Solutions Manager

1 年

I love the colors!

要查看或添加评论,请登录

Grant Harris的更多文章

  • Haystacks and Needles

    Haystacks and Needles

    Strings within strings, aka substrings, aka needles, are searchable by looping through each index of the haystack…

  • How Two Linked Lists Became Merged Into One...

    How Two Linked Lists Became Merged Into One...

    Our journey centers around Dummy, a burgeoning linked list. Dummy is destined to house nodes, each bearing a value and…

  • Palindrome ≠ Emordnilap

    Palindrome ≠ Emordnilap

    While we know the word palindrome is not a palindrome, we sometimes need the computer to confirm whether an integer is…

    2 条评论
  • Permutations!

    Permutations!

    The permutations question wants you to take an array and determine how many permutations you can make. Every time we…

    3 条评论
  • Reverse Reverse

    Reverse Reverse

    For this post, I'll review how to reverse integers, another Leet Code style question. The caveat is that the integers…

    1 条评论
  • A Seat at the Hashtable

    A Seat at the Hashtable

    The solution to the Two Sum question: The two-sum problem solution wants us to provide two indices from an array whose…

    4 条评论

社区洞察