This program is suppose to find all duplicates number of array ‘a’
below . For example in array ‘a’ below , the output should show only
[2,2] and [3,3,3] because there are two number 2 and there are three
number 3 in array ‘a’. The problem is my program show [1], [45], [6] and
[1],[45],[6] should not be in the output.
a=[1,2,3,45,2,3,6,3]
n = a.length
u = n - 1
new_array = []
0…u.times do |number|
xo = []
new_array << xo
x = number + 1
x…n.times do |num|
if (a[number] == a[num])
xo << a[number]
end
end
end
p new_array
#This second part is to eliminate the repeating array containing the
duplicate number. I failed at this too.
pp = new_array.length
ppp = pp - 1
0…ppp.times do |number|
y = number + 1
y…pp.times do |num|
u7 = new_array[number]
u8 = new_array[num]
if u7 == u8
new_array.delete_at(num)
end
end
end
i = 1
while i <= string.length
if arraya[0] == string[i]
arraya << string[i]
else
arrayAnswer << [arraya[0], arraya.length]
arraya = []
arraya << string[i]
end
i += 1
end
p arrayAnswer
puts
i=0
while i < arrayAnswer.size - 1
j=i+1
while j < arrayAnswer.size
if arrayAnswer[i][0] == arrayAnswer[j][0]
x = arrayAnswer[i][1] + arrayAnswer[j][1]
arrayAnswer[i] = [arrayAnswer[i][0], x]
arrayAnswer.delete_at(j)
end
j+=1
end
i+=1
end
p arrayAnswer
i=0
array=[]
while i < string.size - 1
j=i+1
count = 1
while j < string.size
if string[i] == string[j]
count = count + 1
string[j]= nil
end
j+=1
end
array<<[string[i], count]
i+=1
end
p array
i=0
arraya=[]
while i < array.size
if array[i][0] != nil
arraya << array[i]
end
i+=1
end
p arraya
#comment: this is the best version. i actually understand array, string, #and hash really well now. lol
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.