Advertisement
mmayoub

School, 05.09.2017, Ex2, print array without duplicates

Sep 6th, 2017
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1.  
  2. public class Ex2 {
  3.     // Write a Java program to find the duplicate values of an array of integer
  4.     // values
  5.     public static void main(String[] args) {
  6.         int[] arr = { 1, 7, 13, 9, 1, 5, 8, 7, 5, 9, 21 };
  7.         // output: 1 7 13 9 5 8 21
  8.  
  9.         // print source array
  10.         System.out.print("Source array : ");
  11.         for (int i = 0; i < arr.length; i += 1) {
  12.             System.out.print(arr[i] + " ");
  13.         }
  14.         System.out.println();
  15.  
  16.         // print without duplicates
  17.         System.out.print("array without duplicates: ");
  18.         for (int i = 0; i < arr.length; i += 1) {
  19.             boolean print = true;
  20.             for (int j = 0; j < i && print; j += 1) {
  21.                 if (arr[i] == arr[j]) {
  22.                     print = false;
  23.                 }
  24.             }
  25.             if (print) {
  26.                 System.out.print(arr[i] + " ");
  27.             }
  28.         }
  29.  
  30.     }
  31.  
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement