-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnon_repeating_char.rb
More file actions
38 lines (24 loc) · 880 Bytes
/
Copy pathnon_repeating_char.rb
File metadata and controls
38 lines (24 loc) · 880 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def first_non_repeating_char(string)
#put all the characters in the string into two sets
#first set is Unique Characters, second set is #Non-Unique characters
#check to see if the character is in the unique characters set
#if not, place it there, if it is, put it in non_unique characters
#return the first character in unique characters set.
require 'set'
unique = Set.new()
repeat = Set.new()
string.each_char do |s|
if unique.include?(s)
unique.delete(s)
#if true, remove from unique and put into repeat
repeat << s
end
unique << s
end
puts unique.first
end
first_non_repeating_char("string")
first_non_repeating_char("hello")
first_non_repeating_char("jjjijjj")
first_non_repeating_char("abbeabbbeal")
#unique includes all characters, if one of the characters is present, should remove that character