# Category Archives: Arrays

View algorithms on Arrays

## Find the element which appears maximum number of times in an array?

Question: Given an array of n numbers. Give an algorithm for finding the element which appears maximum number of times in the array? Input: 3, 2, 1, 2, 2, 3 Output: 2 One simple and a brute force solution to this is, for each input element check whether there is any element with same value and for each… Read More »

## Write a program to check if there are any duplicated elements in an array? (Method 3)

Question: Given an array of n numbers, give an algorithm for checking whether there are any duplicated elements in the array or not? Input: 4, 8, 15, 16, 23, 42 Output: NO Input: 4, 8, 4, 15, 23, 42 Output: YES Another possible and better way to solve this problem based on a certain condition. Let us assume… Read More »

## Write a program to check if there are any duplicated elements in an array? (Method 2)

Question: Given an array of n numbers, give an algorithm for checking whether there are any duplicated elements in the array or not? Input: 4, 8, 15, 16, 23, 42 Output: NO Input: 4, 8, 4, 15, 23, 42 Output: YES Now the question arises, if we can improve the complexity of the above problem. We discussed the… Read More »

## Write a program to check if there are any duplicated elements in an array?

Question: Given an array of n numbers, give an algorithm for checking whether there are any duplicated elements in the array or not? Input: 4, 8, 15, 16, 23, 42 Output: NO Input: 4, 8, 4, 15, 23, 42 Output: YES This is one of the simplest problems. One obvious solution to this is, exhaustively searching for duplicated… Read More »

## Binary Search

Let us consider the problem of searching a word in a dictionary, in general we directly go to some approximate page [say, middle page] start searching from that point. If the name that we are searching is same then we are done with the search. If the page is before the selected page then we apply the same… Read More »

## Unordered Linear Search

Let us assume that given an array whose elements order is not known. That means the elements of the array are not sorted. In this case if we want to search for an element then we have to scan the complete array and see if the element is there in the given list or not. This requires to… Read More »

## Write a program to reverse an array.

Iterative way: 1) Initialize start and end indexes. start = 0, end = n-1 2) In a loop, swap arr[start] with arr[end] and change start and end as follows. start = start +1; end = end – 1 Time Complexity: O(n) Recursive Way: 1) Initialize start and end indexes start = 0, end = n-1 2) Swap arr[start]… Read More »