how to remove duplicate elements from 2D Array in java?

We can remove duplicate elements from 2D Array in java using for loop.

  • A 2D or multidimensional array is an array of arrays.
  • In first for loop we iterate the array from int i=0 to arr.length-1;
  • From second for loop we iterate the array from int j=1 to arr.length-1;
  • Now validate if(arr[i]==arr[j]) is equals to true, then store the unique elements in arr[i].
  • Print the Arrays.toString(arr[i]) Array elements.
import java.util.Arrays;

public class ArrayTest {
	public static void main(String[] args) {
		int arr[][] = { { 3, 4, 5 }, { 6, 7, 8 }, { 1, 2 }, { 3, 4, 5 } };
		for (int i = 0; i < arr.length; i++) {
			for (int j = 1; j < arr.length; j++) {
				if (arr[i] == arr[j]) {
					System.out.println(Arrays.toString(arr[i]));
				}
			}

		}
	}
}

Output :-
[6, 7, 8]
[1, 2]
[3, 4, 5]