Single Element in a Sorted Array

Single Element in a Sorted Array. Find the element that appears once in a sorted array where every other element appears twice.

Given a sorted array of integers. In this array, every element appears twice except one element which appears only once. Write a code to find the element which appears only once.

For example:

Example 1:

Input: [1, 1, 2, 2, 3, 4, 4, 7, 7]

Output: 3

Except 3 every other element appears twice.

Example 2:

Input: [1, 1, 2, 2, 3, 3, 4, 5, 5]
Output: 4

NOTE – Try to solve this problem in O(logn) time complexity and by using constant space O(1).

In this tutorial, I am going to discuss multiple approaches with their time and space complexity to solve this problem.

I have also added the link of a video tutorial at the end of this post.

Programming video tutorials

Single Element in a Sorted Array Video Tutorial

Find element that appears once in a sorted array

Single Element in a Sorted Array

Single Element in a Sorted Array

Find the Element that Appears once in a Sorted Array using HashMap

Let’s start with the easiest approach to find the element that appears once in sorted array and rest element appears twice. The easiest approach to solve this problem is by using HashMap.

The idea here is to create a map of element and it’s count. Once we know the count of each number of an array, we can easily find the number which appear only once in an array.

The time complexity of this approach is O(n) and it’s space complexity is also O(n).

Find first non-repeating character in a string

Find Element that Appears Once in a Sorted Array using Single Traversal

Let’s improve our previous solution. In our previous approach, we have used extra space to solve this problem.

We can solve this problem simply by traversing an array and compare the element present at i index with i+1 index.

If the element present at both the indexes are same then increment the index by 2. If this condition fails, it means this element has occurred only once. Return this number.

The time complexity of this approach is O(n) and it’s space complexity is O(1).

Find Single Element in a Sorted Array using XOR – Java Code

In this example, we are using the property of XOR.

* XOR (Exclusive or) of a number with itself is zero.

For example:

1 ^ 1 = 0
2 ^ 2 =0

* XOR of a number with zero is number itself.

For example:

3 ^ 0 = 3
2 ^ 0 = 2

Using this approach the number which appears twice is cancelled out when we do the XOR. So, At the end we get the number which occur only once.

The time complexity of this approach is O(n) and it’s space complexity is O(1).

Find Single Element in a Sorted Array using XOR

Find Single Element in a Sorted Array using XOR

Find next greater element in an array

Find Single Element in a Sorted Array using Binary Search – Java Code

Let’s discuss how we can find the element that appears once in sorted array and rest element appears twice using binary search.

Programming questions on Binary Search

We know each element appears twice except one element. All the element has their first occurrence at even index and second occurrence is at odd index. Based on this observation we can use the binary search to solve this problem.

The time complexity of this approach is O(logn) and it’s space complexity is O(1).

Single Element in a Sorted Array Video Tutorial

Find element that appears once in a sorted array

Tagged , , , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.