Friday, August 19, 2022

find duplicate items in array

 Find duplicate items in array in java


This is the java program to find the duplicate elements inside an array.


package com.javadevelopersnotes.controller;

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

public class FindDuplicates {

public static void main(String[] args) {
int arr[] = {1, 2, 3, 4, 5, 5, 6, 7, 8, 8, 9};
int n = arr.length;

Set<Integer> uniqueNumbers = new HashSet<>();
Set<Integer> duplicateNumbers = new HashSet<>();

for (int num : arr) {
if (uniqueNumbers.add(num) == false) {
duplicateNumbers.add(num);
}
}

System.out.println("Below are the unique items");

for (int item : uniqueNumbers) {
System.out.println(item);
}

System.out.println("Below are the duplicate items");

for (int item : duplicateNumbers) {
System.out.println(item);
}

}
}

No comments:

Post a Comment

Ruby Basics

Basics of Ruby   Start Ruby interpret On terminal just type rib ruby_docs $ irb 3 . 0 . 0 : 001 > name = "This is the first ...