Daryan997

2D Array + Duplicates

Oct 2nd, 2020 (edited)
1,187
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package project;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. public class Project {
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner input = new Scanner(System.in);
  10.         int row, col;
  11.         System.out.println("Enter number of rows:");
  12.         row = input.nextInt();
  13.         System.out.println("Enter number of columns:");
  14.         col = input.nextInt();
  15.         int[][] array = new int[row][col];
  16.         ArrayList<Integer> _1dArray = new ArrayList();
  17.         ArrayList<Integer> dupList = new ArrayList();
  18.         System.out.print("You need " + (row * col) + " elements, ");
  19.         for (int i = 0; i < row; i++) {
  20.             System.out.println("Enter " + col + " numbers for row " + i + ":");
  21.             for (int j = 0; j < col; j++) {
  22.                 array[i][j] = input.nextInt();
  23.             }
  24.         }
  25.         System.out.println("Your array is:");
  26.         for (int i = 0; i < row; i++) {
  27.             for (int j = 0; j < col; j++) {
  28.                 System.out.print(array[i][j] + "\t");
  29.             }
  30.             System.out.println("");
  31.         }
  32.         for (int[] r : array) {
  33.             for (int c : r) {
  34.                 _1dArray.add(c);
  35.             }
  36.         }
  37.         for (int i = 0; i < _1dArray.size(); i++) {
  38.             for (int j = i + 1; j < _1dArray.size(); j++) {
  39.                 if (_1dArray.get(i) == _1dArray.get(j)) {
  40.                     if (!dupList.contains(_1dArray.get(i))) {
  41.                      dupList.add(_1dArray.get(i));  
  42.                     }
  43.                 }
  44.             }
  45.         }
  46.         if (dupList.size() > 0) {
  47.             System.out.println(dupList.size() + " duplicates found:");
  48.             for (int dupped : dupList) {
  49.                 System.out.println(dupped);
  50.             }
  51.         }
  52.     }
  53. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×