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
ayushkumarmin203 days ago
unordered_map<int,int>m;
    for( int i=0;i<n;i++){
        m[arr[i]]++;
    }
    for( int i=0;i<n;i++){
        if( m[arr[i]]==1)return arr[i];
    }
        // Complete the function
     return 0;  
0
harayanbas85 days ago
nice
0
chamanlal971961 week 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;
    } 
  
0
niteshkmr3661 week ago
    firstNonRepeating(arr,n){
     var i = 0;
   var bkp = [...arr];
     var map = new Map();
     while(arr.length>0){
         var current = arr.shift();
         if(map.has(bkp[i])){
             map.set(bkp[i],map.get(bkp[i])+1);
         }else{
          //   console.log(bkp[i],'for index',i,'when arr was',arr)
             map.set(bkp[i],1);
         }
         //  if(map.get(bkp[i]) == Math.min(...map.values())){ return bkp[i]}
       i++;
     }
     for (const [key, value] of (map)) {
            if(value == Math.min(...map.values())){ return key}
     }
   return 0;
   }
-3
jainvidhushi292 weeks ago
int res=arr[0];        for(int i=1;i<arr.length;i++)        {            res=res^arr[i];        }        return res;
 
+1
viveksharma733 weeks ago
 unordered_map<int,int >m;              for(int i=0;i<n;i++){           m[arr[i]]++;       }              for(int i=0;i<n;i++){           if(m[arr[i]]==1){               return arr[i];           }       }       return -1;          } 
0
raunakscarlet3 weeks ago
#include<unordered_map>class Solution{   public:   int firstNonRepeating(int arr[], int n)    {        // Complete the function         unordered_map<int,int>umap;         for(int i=0;i<n;i++){             int temp=arr[i];             //umap.insert(temp);             umap[temp]++;         }         for(int i=0;i<n;i++){             auto key=umap.find(arr[i]);             if(key->second==1){                 return key->first;             }                      }   } ;
0
raunakscarlet3 weeks ago
#include<unordered_map>class Solution{   public:   int firstNonRepeating(int arr[], int n)    {        // Complete the function         unordered_map<int,int>umap;         for(int i=0;i<n;i++){             int temp=arr[i];             umap[temp]++;         }         for(int i=0;i<n;i++){             if(umap[arr[i]]==1){                 return arr[i];             }         }   }  };
0
chaitankhilar3 weeks ago
JAVA SOLUTION BY USING HASHMAP class Check{      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],1);          }          else{              hm.put(arr[i],hm.get(arr[i])+1);          }      }     for(int i=0;i<n;i++){         if(hm.get(arr[i])==1){             return arr[i];         }     }     return 0;   }  }
0
swarupcodes1 month ago
class Check{
    
    public int firstNonRepeating(int arr[], int n) 
    { 
        Map<Integer,Integer> map = new LinkedHashMap<>();
        for(Integer x : arr) {
            if(!map.containsKey(x)) {
                map.put(x, 1);
            } else {
                map.put(x, map.get(x)+1);
            }
        }
        for(Map.Entry<Integer,Integer> entry : map.entrySet()) {
            if(entry.getValue() == 1) return entry.getKey();
        }
        return 0;
    }  
}
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.