Finding a non-duplicate element in an array

I am stuck in the following program:

I have an integer input array that has only one non-duplicate number, for example {1,1,3,2,3}. The output should display a non-duplicate element, i.e. 2.

So far I have done the following:

public class Solution {

    public int singleNumber(int[] arr){
        int size = arr.length;
        int temp = 0;
        int result = 0;
        boolean flag = true;
        int[] arr1 = new int[size];

        for(int i=0;i<size;i++){
            temp = arr[i];
            for(int j=0;j<size;j++){
                if(temp == arr[j]){
                    if(i != j)
                    //System.out.println("Match found for "+temp);
                    flag = false;
                    break;
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {

        int[] a = {1,1,3,2,3};
        Solution sol = new Solution();

        System.out.println("SINGLE NUMBER : "+sol.singleNumber(a));
    }
}

Constraining a solution in an array is preferable. Avoid using collections, cards.

+4
source share
8 answers

Since this is almost certainly a training exercise, and because you are very close to its correct right, here is what you need to change to make it work:

  • flag - true .
  • flag, - flag true, ; .
+4
public class NonRepeatingElement {

public static void main(String[] args) {

    int result =0;
    int []arr={3,4,5,3,4,5,6};
    for(int i:arr)
    {
        result ^=i;
    }

    System.out.println("Result is "+result);
}

}
+4
From Above here is the none duplicated example in Apple swift 2.0

func noneDuplicated(){    
            let arr = [1,4,3,7,3]    
                 let size = arr.count  
                 var temp = 0  
                 for i in 0..<size{  
                   var flag = true  
                   temp = arr[i]  
                     for j in 0..<size{  
                     if(temp == arr[j]){  
                        if(i != j){  
                            flag = false  
                            break  
                        }   
                    }   
                }    
                if(flag == true){   
                print(temp + " ,")   
                }  
            }  
        }

// output : 1 , 4 ,7
// this will print each none duplicated 
+1

,

public class Solution {

    public int singleNumber(int[] arr){
        int size = arr.length;
        int temp = 0;
        int result = 0;
        boolean flag = true;
        int[] arr1 = new int[size];

        for(int i=0;i<size;i++){
            temp = arr[i];
            int count=0;
            for(int j=0;j<size;j++){
                if(temp == arr[j]){
                    count++;
                }
            }
            if (count==1){
               result=temp;
               break;
            }
        }
        return result;
    }
0

Try:

public class Answer{
   public static void main(String[] args) {
    int[] a = {1,1,3,2,3};
    int[] b =new int[a.length]; 
    //instead of a.length initialize it to maximum element value in a; to avoid
    //ArrayIndexOutOfBoundsException
    for(int i=0;i<a.length;i++){
      int x=a[i];
      b[x]++;
    }
     for(int i=0;i<b.length;i++){
       if(b[i]==1){
       System.out.println(i); // outputs 2
       break;
       }
     }
   }
}

PS: java, C.

0

, , , ( 2). , , , , for, , , - , , , .

public class Solution {

    public int singleNumber(int[] arr){
        int size = arr.length;
        int temp = 0;
        int result = 0;
        int temp2 = 0;
        int temp3 = 0;
        boolean flag = true;
        int[] arr1 = new int[size];

        for(int i=0;i<size;i++){
            temp = arr[i];
            temp2 = temp*temp;
            for(int j=0;j<size;j++){
                temp3 = temp*arr[j];
                if(temp2==temp3 && i!=j)
                    j=arr.length
                if(temp2 != temp3 && j==arr.length){
                    //System.out.println("Match found for "+temp);
                    flag = false;
                    result = temp;
                    break;
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {

        int[] a = {1,1,3,2,3};
        Solution sol = new Solution();

        System.out.println("SINGLE NUMBER : "+sol.singleNumber(a));
    }
}
0

@dasblinkenlight...

public class Solution {

    public int singleNumber(int[] arr){
        int size = arr.length;
        int temp = 0;
        int result = 0;

        int[] arr1 = new int[size];

        for(int i=0;i<size;i++){
            boolean flag = true;
            temp = arr[i];
            for(int j=0;j<size;j++){
                if(temp == arr[j]){
                    if(i != j){
//                  System.out.println("Match found for "+temp);
                    flag = false;
                    break;
                    }
                }

            }
            if(flag == true)
                result = temp;
        }
        return result;
    }

    public static void main(String[] args) {

        int[] a = {1,1,3,2,3};
        Solution sol = new Solution();
        System.out.println("SINGLE NUMBER : "+sol.singleNumber(a));
    }
}

if(i != j) . .

0

, .

  • Quick Sort. nlogn.
  • . (0, 2,..) (1, 3,...). , .

, :

a) , 'mid.

b) " ", arr [mid] arr [mid + 1]. , .

c) If the "middle is odd", compare arr [mid] and arr [mid-1]. If both are the same, then the required item is after mid to mid.

0
source

Source: https://habr.com/ru/post/1569551/


All Articles