Find the first non-repeating element in a given array of integers
We are given an array of
Example 1
Input: {1, 2, 3, 1, 2, 3, 4}
Output: 4
In this array,
Example 2
Input: {1, 2, 3, 1, 3, 5}
Output: 2
In this array,
The corner cases of this problem are as follows:
Example 3
Input: {-1, -2, 0, 2, 3}
Output: -1
There are no repeated elements in this array. Therefore, we return the first element of the array as it is the first non-repeating element.
Example 4
Input: {-1, -2, -3, -1, -2, -3}
Output: No non-repeating elements.
All elements are repeated in this array. Therefore, there are no non-repeating elements, and we print that on the screen.
Algorithm
We iterate the entire array from left to right and check every element for repetitions. If we find a repetition, we move to the next element and repeat the same process.
If we do not find any repetition for an element, then we return that element because that is the first non-repeating element in the array.
If we do not find any non-repeating element, we print an appropriate message on the screen.
Code example
Let's look at the code below:
#include <iostream>using namespace std;int firstNonRepeatingElem(int arr[], int len) {for(int i = 0; i < len; i++) // iterates over every element of the arrayfor(int j = 0; j < len; j++) { // checks if that element is repeating in the arrayif(arr[i] == arr[j] && i != j) // breaks the loop if a repeating element is foundbreak;if(j == len - 1) // if we reach the last index of the array andreturn i; // haven't found a repetition for the element at index i,} // then we return the index ireturn -1; // if all the elements in the array are repeating then we return a -1 flag.}// Driver codeint main() {int inp_arr[] = {1, 2, 3, 1, 2, 3, 4};int length = sizeof(inp_arr)/sizeof(int);int answer = firstNonRepeatingElem(inp_arr, length); // calling our functionif(answer == -1) // if the flag is returned, we print the following line.cout << "No non-repeating elements.";elsecout << inp_arr[answer]; // if a non-repeating element is found, we return the element atreturn 0; // the index returned from the function.}
Code explanation
Line 5–6: The outer
forloop iterates over the entire array and the innerforloop checks the repetition of that element in the array.Lines 7–8: If a repeating element is found, this
ifcondition breaks the loop and moves to the element at next index to check for repetition.Lines 10–11: If no repetition is found for the element at index
i, then we return that index of the array (as it is the first non-repeating element).Line 14: If all of the elements in the array are repeating, then we return a
-1flag to indicate that no non-repeating elements are found.Lines 24–27: Our function returns the index of the array at which the non-repeating element is present or it returns a
-1flag if all elements are repeating. Thisifcondition checks for the flag and prints the appropriate result on the screen.
Time complexity
We iterate over the array of
Space complexity
We did not use any temporary data structures to store intermediate results. So, the space complexity of this algorithm is
Free Resources