Remove duplicates in 2d array

php compare two multidimensional arrays and remove duplicates
php find duplicates in multidimensional array
find duplicate elements in 2d array in c
remove duplicates from multidimensional array php
javascript remove duplicate objects from array
remove duplicates from 2d array python
powershell remove duplicates from multidimensional array
remove duplicates from array javascript

I want to remove duplicate row in a 2d array . i tried the below code .but it is not working . please help me .

Input :


output should be :


Code :

package employee_dup;

import java.util.*;

public class Employee_dup {

    public static void main(String[] args)
        boolean Switch = true;
        System.out.println("Name  ID  Dept ");
        String[][] employee_t = {{"1","ram","Mech"},{"1","siva","Mech"},{"1","gopi","Mech"},{"4","jenkat","Mech"},{"5","linda","Mech"},{"1","velu","Mech"}};
        int g = employee_t[0].length;
        String[][] array2 = new String[10][g];
        int rows = employee_t.length;
        Arrays.sort(employee_t, new sort(0));

        for(int i=0;i<employee_t.length;i++){  
            for(int j=0;j<employee_t[0].length;j++){  

                System.out.print(employee_t[i][j]+" ");  

        List<String[]> l = new ArrayList<String[]>(Arrays.asList(employee_t));

        for(int k = 0 ;k < employee_t.length-1;k++)
            if(employee_t[k][0] == employee_t[k+1][0])
                System.out.println("same value is present");  
                array2 = l.toArray(new String[][]{});

        System.out.println("Name  ID  Dept ");
        for(int i=0;i<array2.length;i++){  
            for(int j=0;j<array2[0].length;j++){  

                System.out.print(array2[i][j]+" ");  

class sort implements Comparator {
    int j;
    sort(int columnToSort) {
        this.j = columnToSort;
    //overriding compare method
    public int compare(Object o1, Object o2) {
        String[] row1 = (String[]) o1;
        String[] row2 = (String[]) o2;
        //compare the columns to sort
        return row1[j].compareTo(row2[j]);

First I sorted the array based on column one ,then tried to remove duplicates by checking the first column elements and seconds column elements but it is not removing the required column but remove other columns.

You may give this solution a try:

public static void main(String[] args) {
    String[][] employee_t = {
            {"1","gopi","Mech"} };
    System.out.println("ID Name   Dept");
          .forEach(row -> System.out.printf("%-3s%-7s%s\n", row.get(0), row.get(1), row.get(2)));


ID Name   Dept
1  ram    Mech
1  siva   Mech
1  gopi   Mech

How it works: comparing arrays does rely on instance equality and not on comparing contained elements by equals. Hence converting each row of your 2D array into a List will enable you to compare lists, which takes equals of the elements contained into account.

The Java Stream API does provide a method distinct which relies on equals and will remove all duplicates for you.

[Solved] Removing duplicate values in 2d array, If you want to remove all duplicates, so you are left with the distinct rows only, then the simplest way if to sort the input, so that duplicates are  If you want to remove all duplicates, so you are left with the distinct rows only, then the simplest way if to sort the input, so that duplicates are next to each other. that way, it's trivial to remove duplicated rows. C and C++ both include library routines to sort an array.

Based on your code. Maybe it is not the BEST solution but it works.

public static void main(String[] args) {

    System.out.println("Name  ID  Dept ");
    // I added duplicated rows
    String[][] inputArray = {
            { "1", "ram", "Mech" }, 
            { "1", "siva", "Mech" }, 
            { "1", "gopi", "Mech" }, 
            { "1", "gopi", "Mech" }, 
            { "4", "jenkat", "Mech" },
            { "5", "linda", "Mech" }, 
            { "1", "velu", "Mech" },
            { "1", "velu", "Mech" }

    // I will add all rows in a Set as it doesn't store duplicate values
    Set<String> solutionSet = new LinkedHashSet<String>();

    // I get all rows, create a string and insert into Set
    for (int i = 0 ; i < inputArray.length ; i++) {
        String input = inputArray[i][0]+","+inputArray[i][1]+","+inputArray[i][2];

    // You know the final size of the output array
    String[][] outputArray = new String[solutionSet.size()][3];

    // I get the results without duplicated values and reconvert it to your format
    int position = 0;
    for(String solution : solutionSet) {
        String[] solutionArray = solution.split(",");

        outputArray[position][0] = solutionArray[0];
        outputArray[position][1] = solutionArray[1];
        outputArray[position][2] = solutionArray[2];


    System.out.println("Name  ID  Dept ");
    for (int i = 0; i < outputArray.length; i++) {
        for (int j = 0; j < outputArray[0].length; j++) {

            System.out.print(outputArray[i][j] + " ");


Javascript: Remove duplicates of multidimensional array · GitHub, Javascript: Remove duplicates of multidimensional array. remove_duplicates_array_multi.js. // for browser using, this code requires javascript  I'm importing a txt file, and then filling the 2D array with the points, all that is working fine. I can't for the life of me get it working where I can find duplicate points and then basically create a new array with only single points, no duplicates. Since they're coordinates I only want to remove a duplicate POINT, so both X and Y have to match.

I have posted what I think is a readable and easy to maintain solution.

I decided to use distinct from Stream which is part of Java 8

Returns a stream consisting of the distinct elements (according to Object.equals(Object)) of this stream. -


class Main {
    public static void main(String[] args)
        //Create a list of Employee objects
        List<Employee> employeeList = new ArrayList<Employee>();
        Employee e1 = new Employee(1, "ram", "mech");
        Employee e2 = new Employee(1, "ram", "mech");
        Employee e3 = new Employee(2, "gopi", "csc");
        Employee e4 = new Employee(2, "gopi", "civil");


        System.out.println("Before removing duplicates");;

        //This is where all the magic happens.
        employeeList =;

        System.out.println("\nAfter removing duplicates");;


Before removing duplicates
Employee [valA=1, valB=ram, valC=mech]
Employee [valA=1, valB=ram, valC=mech]
Employee [valA=2, valB=gopi, valC=csc]
Employee [valA=2, valB=gopi, valC=civil]

After removing duplicates
Employee [valA=1, valB=ram, valC=mech]
Employee [valA=2, valB=gopi, valC=csc]
Employee [valA=2, valB=gopi, valC=civil]


//This is just a regular POJO class.
class Employee {

    int valA;
    String valB, valC;

    public Employee(int valA, String valB, String valC){
        this.valA = valA;
        this.valB = valB;
        this.valC = valC;

    public Employee(Employee e) {
        this.valA = e.valA;
        this.valB = e.valB;
        this.valC = e.valC;

    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + valA;
        result = prime * result + ((valB == null) ? 0 : valB.hashCode());
        result = prime * result + ((valC == null) ? 0 : valC.hashCode());
        return result;
    public boolean equals(Object obj) {

        if(obj instanceof Employee && ((Employee)obj).hashCode() == this.hashCode()){
            return true;

        return false;

    public String toString() {
        return "Employee [valA=" + valA + ", valB=" + valB + ", valC=" + valC + "]";

array_unique for multidimensional array – James' Desk, array_unique removes duplicates from only flattened one dimensional array. But still it can be used in a multi-dimensional array with a small  I want to remove duplicate Row in this 2D array. Remove Duplicate Row From 2d Array LabVIEW Engineer- Power System Automation

Pre Java - 8 solution. May not be the best way. But a quick solution which works..

String[][] records = {
            {"1","gopi","civil"} };

    List<String[]> distinctRecordsList = new ArrayList<String[]>();
    for(String[] record : records){
            boolean sameValue = false;
            for(String[] distinctRecord : distinctRecordsList){
                int distinctRecordFields = distinctRecord.length;
                    for(int k=0;k<distinctRecordFields;k++){
                        sameValue = record[k].equalsIgnoreCase(distinctRecord[k]);
                    throw new Exception("Can't compare the records");
        }else if(distinctRecordsList.size()==0)
    Object[] distRecObjects = distinctRecordsList.toArray();
    String[][] distinctRecordsArray = new String[distRecObjects.length][];

    int i=0;
    for(Object distRecObject : distRecObjects){
        distinctRecordsArray[i] = (String[]) distRecObject;

Removing row or column wise duplicates from matrix of characters , Removing row or column wise duplicates from matrix of characters separated · Kth smallest element in a row-wise and column-wise sorted 2D array | Set 1  1) Remove duplicates from an array using a Set. A Set is a collection of unique values. To remove duplicates from an array: First, convert an array of duplicates to a Set. The new Set will implicitly remove duplicate elements. Then, convert the set back to an array. The following example uses a Set to remove duplicates from an array:

Removing duplicates from an array with vanilla JavaScript, Today, I want to show you how a few approaches we can use to remove duplicate items from an array with vanilla JS. An example Remember  We can remove duplicate element in an array by 2 ways: using temporary array or using separate index. To remove the duplicate element from array, the array must be in sorted order. If array is not sorted, you can sort it by calling Arrays.sort(arr) method.

Removing Duplicate Arrays from an Array of Arrays, How will we both identify the duplicate arrays and also remove those duplicates to Our bigArray is a two-dimensional array where we have an array whose  Remove duplicates from sorted array. Given a sorted array, the task is to remove the duplicate elements from the array. Examples: Input : arr [] = {2, 2, 2, 2, 2} Output : arr [] = {2} new size = 1 Input : arr [] = {1, 2, 2, 3, 4, 4, 4, 5, 5} Output : arr [] = {1, 2, 3, 4, 5} new size = 5.

Remove Duplicates From Multidimensional Array, This php tutorial wil hep to remove duplicate values from multi dimensional array using php.I am using array_unique() function to remove  Remove duplicate elements. Maintain the order of elements added to it. Java program to remove duplicates in array using LinkedHashSet. In given example, numbers is an integer array which has duplicate numbers 1, 3 and 5. We add all elements to LinkedHashSet, and then get back the content in array.

Removing duplicates from an array quickly, You can use the indexOf function to check if a value exists in an array. This would simplify your code greatly: Array.prototype.unique = function() { var a = []; for ( i  Simple Program to remove Duplicate Element in an Array. Below is a program to find and remove any duplicate element present in the specified array.

  • Arrays.sort(employee_t, new sort(0)); What do you mean by 'new sort(0)' ?
  • Do you want help to fix your code, or can I post an answer of how you could do this a lot more clean?
  • Hint : You don't need to sort to remove duplicates.
  • Your code was quite unreadable due to wrong indentation. This time I fixed it for you; next time please have your IDE do it, it’s much easier.
  • @OleV.V. Thanks, when i posted a comment, the code of sort method wasn't included.
  • A good idea to have an Employee class to model employees rather than the subarrays. The asker, knowing the domain, should be able to find better variable names than valA, valB and valC.
  • Yes, I agree that it is terrible variable names. OP did not mention what his values actually represented. :)
  • Any idea what the performance characteristics are from the distinct operation on that stream?
  • @skubski, since distinct() cannot rely on hash code or such, I would expect quadratic time, O(n^2). It’s not something I know.