Simple Java Solution


  • -1
    P
                                   public class Solution {
                                         public int singleNumber(int[] nums) {
                                                           int p=0;
    	                                          Arrays.sort(nums);
    	                                          if(nums.length==1)
    	                                         {   
                                                           int i=0;
    		                                   p=nums[i];
    		                                   return p;
    	                                         }
    	                                         if(nums.length==2)
    	                                        {
    		                                 for(int i=0;i<=nums.length-1;)
    		                               {
    			                           if(nums[i]!=nums[i+1])
    				                   {
    					                 p=nums[i];
    					                   return p;
    					
    				                     }
    				                        else
    				                    {
    					                 i+=2;
    				                     } 
    		                                  }
    	                                      }
    	                                      else if(nums.length>2)
    	                                    {
    	                                          for(int i=0;i<nums.length;)
    	                                   {   
    		                                if(i==nums.length-1)
    			                  {
    				               p=nums[i];
    				               return p;
    			                   }
    		                           else if(nums[i]!=nums[i+1])
    		                         {
    			                      p=nums[i];
    			                           i++;
    		                         }
    		
    		                    else
    		                      {
    			                     i+=2;
    		                      }
    		
    		 
    				 
    	                                        }
    	                                       }
                                                   return p;
                                                       }
                                                      }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.