Non-Repeating Element | Practice | GeeksforGeeks

Find the first non-repeating element in a given array arr of N integers.
Note: Array consists of only positive and negative integers and not zero.
Example 1:
Input : arr[] = {-1, 2, -1, 3, 2}
Output : 3
Explanation:
-1 and 2 are repeating whereas 3 is 
the only number occuring once.
Hence, the output is 3. 
 
Example 2:
Input : arr[] = {1, 1, 1}
Output : 0


Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function firstNonRepeating() that takes an array (arr), sizeOfArray (n), and returns the first non-repeating element. The driver code takes care of the printing.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= N <= 107
-1016 <= Ai <= 1016
{Ai !=0 }
 
0
vishutyagi76 days ago
0.6 sec c++
unordered_map<long long,long long> m;       unordered_map<long long,long long> m1;       int i,k=0;       for(i=0;i<n;i++)       {           m[arr[i]]++;       }       auto it=m.begin();       while(it!=m.end())       {           if(it->second==1)           {               m1[it->first]=1;           }           ++it;       }       for(i=0;i<n;i++)       {           if(m1[arr[i]]==1)           {               return arr[i];               k=1;               break;           }       }       if(k==0)       {           return 0;       }
0
rajurajampet072 weeks ago
0.3 sec in pythonfrom collections import Counterclass Solution:   def firstNonRepeating(self, arr, n):        # Complete the function       c=Counter(arr)       for i in arr:           if c[i]==1:               return i       return 0          
0.3 sec in pythonfrom collections import Counterclass Solution:   def firstNonRepeating(self, arr, n):        # Complete the function       c=Counter(arr)       for i in arr:           if c[i]==1:               return i       return 0          
0
viveksinghbais77063 weeks ago
 int firstNonRepeating(int arr[], int n)    {        unordered_map<int,int>mp;              for(int i=0;i<n;i++)       {           mp[arr[i]]++;       }              for(int i=0;i<n;i++)       {           if(mp[arr[i]]==1)           {               return arr[i];           }       }              return 0;          } 
+1
anamjamadar4643 weeks ago
JAVA CODE:
 public int firstNonRepeating(int arr[], int n)    {       HashMap<Integer,Integer>map=new HashMap<>();             for(int i=0;i<n;i++)      {          if(map.containsKey(arr[i]))          map.put(arr[i],map.get(arr[i])+1);          else          map.put(arr[i],1);                }      int ans=-1;      for(int i=0;i<n;i++)      {          if(map.get(arr[i])==1)          {          ans=arr[i];          break;          }     }     return ans;   }     }
+1
harshpandeyalfa23 weeks ago
C++
(0.49 sec)
int firstNonRepeating(int arr[], int n)    {        unordered_map<int,int>mp;       for(int i=0;i<n;i++)       {           mp[arr[i]]++;       }       // int i=0;       for(int i=0;i<n;i++)       {           if(mp[arr[i]] ==1 )            return arr[i];        }       return 0;   } 
0
mayurxxivk3 weeks ago
CorrectAnswer.🌻
Total Time Taken:1.25/2.52
  public int firstNonRepeating(int arr[], int n) 
    { 
        LinkedHashMap<Integer , Integer> hs  = new LinkedHashMap<>();
        
        for(int i =0 ; i<n ; i++)
        {
            if(hs.containsKey(arr[i]))
            hs.put(arr[i] , hs.get(arr[i])+1);
            else
            hs.put(arr[i] , 1);
            
        }
        
        for(Map.Entry<Integer, Integer>entry : hs.entrySet())
        {
            if(entry.getValue() == 1)
            return entry.getKey();
        }
        
        return 0;
    }
0
aniketsaraswat1121 month ago
int firstNonRepeating(int arr[], int n) {
        unordered_map<int,int> umap;
        for(int i = 0; i<n; i++) umap[arr[i]]++;
        for(int i = 0; i<n; i++) 
            if(umap[arr[i]] == 1) return arr[i];
        return 0;
    } 
+1
rahul8781 month ago
-----------------JAVA CODE SOLUTION------------------------
public int firstNonRepeating(int arr[], int n)    {        HashMap<Integer,Integer> hm=new HashMap<>();       for(int i=0;i<n;i++){           if(hm.containsKey(arr[i]))           hm.put(arr[i],hm.get(arr[i])+1);           else            hm.put(arr[i],1);       }             for(int i=0;i<n;i++){          if(hm.get(arr[i])==1)            return arr[i];      }              return 0;   }  
0
maac_shah1 month ago
JAVA LinkedHashMap Solution
class Check{
    
    public int firstNonRepeating(int arr[], int n) 
    { 
        // Complete the function
        LinkedHashMap<Integer,Integer> map = new LinkedHashMap<>();
        
        for(int i=0;i<n;i++)
        {
            Integer val = map.get(arr[i]);
            
            if(val == null)
            {
                map.put(arr[i],1);
            }
            else
            {
                map.replace(arr[i],val+1);
            }
        }
        
        int min = Integer.MAX_VALUE;
        
        for(Map.Entry<Integer,Integer> entry : map.entrySet())
        {
            if(entry.getValue()<=1)
            {
                min = entry.getKey();
                break;
            }
        }
        
        for(int i=0;i<n;i++)
        {
            if(min==arr[i])
            {
                return arr[i];
            }
        }
        return 0;
    }  
    
}
0
shubham501 month ago
    int firstNonRepeating(int arr[], int n)    {       unordered_map<int , pair<int,int>> m;            for(int i=0;i<n;i++)      {          m[arr[i]].first = m[arr[i]].first + 1 ;          m[arr[i]].second = i;      }            int flag = INT_MAX;      for( auto v : m)      {          if(v.second.first == 1)          {              if(flag>v.second.second)               flag = v.second.second;          }      }
      return arr[flag];         } 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.