Unique elements

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.5 kB
4
Indexable
import java.io.*;
import java.util.*;

public class si1{

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        int n;
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        int[] ar = new int[n + 1];
        for(int i = 0;i < n;i++){
            ar[i] = sc.nextInt();
        }
        
        int[] uniqueArray = new int[n];
        for(int k = 0;k < n;k++){
            uniqueArray[k] = Integer.MIN_VALUE;
        }
        int k = 0,placeHolder = 0;
        for(int i = 0;i < n;i++){ // i->1 & 4
            int count = 0;
            for(int j = 0;j <= n;j++){ //ar[j] -> 5,4,10,9,21,4
                if(j == n){
                    uniqueArray[k] = placeHolder;
                    k++;
                }
                else if(ar[i] == ar[j] && count == 0){ //4==4
                    placeHolder = ar[i]; // 4
                    count++; // 1
                }
                else if(ar[i] == ar[j] && count != 0){
                    break;
                }
                else if(ar[i] != ar[j]){
                    continue;
                }
                
            }
        }
        for (int i = 0; i < uniqueArray.length; i++) {
            if(uniqueArray[i] != Integer.MIN_VALUE){
            System.out.print(uniqueArray[i] + " ");
            }
        }
    }
}