11. Write a method to remove duplicate elements from an array without using uniq.
Required Input:
[1, 2, 2, 3, 4, 4, 5]
Expected Output:
[1, 2, 3, 4, 5]
Code In Ruby
# Predefined array
array = [1, 2, 2, 3, 4, 4, 5]
# Your logic here
Run Code?
Click Run Button to view compiled output
12. Create a program to count the frequency of words in a sentence.
Required Input:
"hello world hello"
Expected Output:
hello: 2
world: 1
Code In Ruby
# Predefined sentence
sentence = "hello world hello"
# Your logic here
Run Code?
Click Run Button to view compiled output
13. Write a method to find the common elements in three arrays.
Required Input:
[1, 2, 3], [2, 3, 4], [3, 4, 5]
Expected Output:
[3]
Code In Ruby
# Predefined arrays
array1 = [1, 2, 3]
array2 = [2, 3, 4]
array3 = [3, 4, 5]
# Your logic here
Run Code?
Click Run Button to view compiled output
14. Implement a method to check if a string is a valid palindrome, ignoring case and non-alphanumeric characters.
Required Input:
"A man, a plan, a canal: Panama"
Expected Output:
true
Code In Ruby
# Predefined string
string = "A man, a plan, a canal: Panama"
# Your logic here
Run Code?
Click Run Button to view compiled output
15. Create a program to convert a Roman numeral to an integer.
Required Input:
"XIV"
Expected Output:
14
Code In Ruby
# Predefined Roman numeral
roman = "XIV"
# Your logic here
Run Code?
Click Run Button to view compiled output
16. Write a method to calculate the sum of digits of a given number.
Required Input:
12345
Expected Output:
15
Code In Ruby
# Predefined number
number = 12345
# Your logic here
Run Code?
Click Run Button to view compiled output
17. Create a program to group anagrams from an array of strings.
Required Input:
["eat", "tea", "tan", "ate", "nat", "bat"]
Expected Output:
[["eat", "tea", "ate"], ["tan", "nat"], ["bat"]]
Code In Ruby
# Predefined array of strings
words = ["eat", "tea", "tan", "ate", "nat", "bat"]
# Your logic here
Run Code?
Click Run Button to view compiled output
18. Write a method to find all pairs in an array that sum up to a given value.
Required Input:
Array: [1, 2, 3, 4, 5], Sum: 6
Expected Output:
[[2, 4], [1, 5]]
Code In Ruby
# Predefined array and target sum
array = [1, 2, 3, 4, 5]
target_sum = 6
# Your logic here
Run Code?
Click Run Button to view compiled output
19. Implement a method to calculate the power of a number using recursion.
Required Input:
Base: 2, Exponent: 3
Expected Output:
8
Code In Ruby
# Predefined base and exponent
base = 2
exponent = 3
# Your logic here
Run Code?
Click Run Button to view compiled output
20. Create a class BankAccount with deposit and withdraw methods.
Required Input:
Initial Balance: 1000, Operations: deposit(500), withdraw(300)
Expected Output:
Balance after deposit: 1500
Balance after withdrawal: 1200
Code In Ruby
# Predefined initial balance and operations
initial_balance = 1000
# Your logic here
Run Code?
Click Run Button to view compiled output