?> Process behind finding unique array items – Camel Dev

Process behind finding unique array items

Some programming algorithm process using this process as their part. Finding unique item/items in listed array. In Ruby we could use uniq function to delete duplicate value in an array list. To get more clear illustration, please check example below.

a = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]

puts a # result should be [1, 2, 3, 4, 5]

Have you ever think about manual process finding unique array item list?

Someone comment giving this advice to train algorithm logic in a forum. Sound like reinvent the wheel but I think this good idea make our logic better. Why not to try? Challenge accepted.

new_item = []

a = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5]

a.each do |item|
  if new_item.include?(item) == false

puts new_item.to_s # result should be [1, 2, 3, 4, 5]

include is function to check an item exist or not in array list.

push is function for adding new array item.

One of real life situation to implement unique item is finding unique costumer from online shop in this month.  Another example is detect how many member of a marketplace. Although both example could be done with database operation command. In this case you just need to think that we compare in outside of database.

Main process of getting unique item from array list are check one by one and compare with collected unique list. If checked item not found, add it to collected list. Finally, you can fasten this flow in any process which needed.


Add a Comment

Your email address will not be published. Required fields are marked *