Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 11/17/2018 By admin

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

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

Uncategorized

Post navigation

Previous Post: Manual Testing | Make Selenium Easy
Next Post: Make Selenium Easy | Make Selenium Easy – Selenium Tutorials : End To End

Related Posts

Hierarchy of Selenium Classes and Interfaces – Make Selenium Easy Uncategorized
Postman Tutorial Part 17 – Creating a Collection In Postman Uncategorized
Postman Tutorial Part 3 – Sending First GET Request in Postman Uncategorized
January 2019 – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
How To Load URL In Selenium WebDriver – get() & navigate() Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark