Categories: Frequently Asked Java Programs In Interview

Java Programs 17: Java Program to Find Duplicate Elements in Each Row of 2D Array

Hello Programers,

Problem:

How to find duplicate values in each row of 2D array. For example , we have 2d array as below:

4 67 34 90
9 4 11 100
6 12 9 4
78 87 4 2

Output should be : 4 , Because 4 is common in each row.

Solution:

This was really brainstorming as I want to avoid multiple for loops to solve this problem. My logic to solve above problem is as below:

  1. I assume all elements in first row are duplicate elements i.e. exists in all rows.
  2. I store all elements of first row in a HashSet say firstRow. I used HashSet just to add only unique elements as a row can have duplicate elements as well.
  3. Now I take all elements of second row and store them in a different HashSet say secondRow.
  4. Now I take intersection of firstRow and secondRow. It will return me only elements which are common in both rows.
  5. If size of firstRow is zero, we will stop further iteration as it means there is no element which is common in all rows.
  6. If size of firstRow is not zero, repeat step 4 for next row i.e. 3rd row.

Understand logic in Pic:

 

 

Java Programs:

package Arrays;

import java.util.HashSet;
import java.util.Set;

public class DuplicateElementInRows {

public static void main(String[] args) {

// An input array
int arrayValues[][] = { { 4, 67, 34, 90 }, { 9, 4, 11, 100 }, { 6, 12, 9, 4 }, { 78, 87, 4, 2 } };

// A set to store expected duplicates
Set expectedDuplicates = new HashSet();

// To store next row elements
Set deleteRow = new HashSet();

boolean duplicates=true;

// Iteration starts from here
for (int i = 0; i

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Recent Posts

How To Verify Maximum Character Limit of an Input Box Through Selenium

Hello Folks, Recently a guy asked me this question which he was asked in an interview in IBM. What the…

13 hours ago

API Testing Tutorial Part 15 – Sending GET Request With Params in Postman

Hello Folks, As part of our API Testing series, we will see “Sending GET request with params in Postman”. In last…

4 days ago

TestNG Tutorials 62: Dependency in TestNG – Types of Dependencies in TestNG

We have learnt in previous posts regarding establishing relationship between test methods. You can go through them below: Dependency in…

7 days ago

TestNG Tutorials 61: Dependency in TestNG – Creating Dependency Among Test Methods – DependsOnGroup

In previous post, We have learnt to Establish dependency among test methods. In this post, we will see another concept…

7 days ago

TestNG Tutorials 60: Dependency in TestNG – Creating Dependency Among Test Methods – DependsOnMethod

We are going to see another important concept or feature provided by TestNG. This feature is called as Dependency. Suppose,…

1 week ago

TestNG Tutorials 59: DataProvider in TestNG – Running DataProvider Method in Parallel – Parallel DataProvider Method

A Test method is run for all data set provided by a DataProvider method which is by default one after…

2 weeks ago