Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7706059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:01:17+00:00 2026-06-01T00:01:17+00:00

Possible Duplicate: How to find a duplicate of length 5 in a single array.

  • 0

Possible Duplicate:
How to find a duplicate of length 5 in a single array. Java

I am trying to see if there are any duplicates in the array.

import java.util.Hashtable;

public class test {
    static final int COUNT = 10;

    static Hashtable<String, Integer> compareSet = new Hashtable<String, Integer>();
    static String groupInteger = "";
    static long arr[] = new long[5];
    static int st = 1;
    static int end = 56;
    static double t1;
    static double t2;

    static int ctr = 0;

    public static void main(String[] args) {
        t1 = System.currentTimeMillis();
        for (int n = 0; n < COUNT; n++) {
            for (int i = 0; i < arr.length; i++) {
                arr[i] = (long) ((Math.random() * (end - st + 1)) + st);

            }
            for (int i = 0; i < 5; i++) {
                groupInteger += arr[i];
                if (i % 5 == 0) {
                    System.out.println();
                    if (compareSet.containsKey(groupInteger)) {
                        ctr++;
                        System.out.println("duplicate found ");
                        int currentCount = compareSet.get(groupInteger);
                        compareSet.put(groupInteger, currentCount + 1);

                    } else {
                        compareSet.put(groupInteger, 1);
                    }
                    groupInteger = "";
                }
                System.out.print(arr[i]);
            }
        }
        t2 = System.currentTimeMillis();
        System.out.println();
        System.out.println();
        System.out.println("\t" + "Total run time is " + ((t2 - t1)) + "ms");
        System.out.println(compareSet);
    }
}

the console:

12  23  8   44  23
28  13  39  49  53
37  40  16  53  48
6   45  14  52  20
32  4   41  10  38
38  29  25  21  13
16  34  45  26  11
22  33  54  21  10
40  34  53  37  50
20  26  48  32  51

I want to be able to check if there are duplicate rows.
I cant seem to finagle this code around to work properly.
Please let me know if you see something! Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:01:19+00:00Added an answer on June 1, 2026 at 12:01 am

    You are using inappropriate classes for that, if you want to find “any” duplicates
    Use HashSet<String>

    And your code is totally buggy. The if-then part must be after the for, without if.

    HashSet<String> compareSet = new HashSet<String> ();
    ...
    groupInteger.empty ();
    for (int i = 0; i < 5; i++) {
      groupInteger += arr[i] + " ";
    }
    System.out.println(groupInteger);
    if (compareSet.contains(groupInteger)) {
        System.out.println("duplicate found ");
    } else {
        compareSet.add(groupInteger);
    }
    

    And if you want to “count” duplicates, I would use HashMap

    HashMap<String, Integer> compareSet = new HashMapt<String, Integer> ();
    ...
    Integer act = compareSet.get(groupInteger);
    if (act != null) {
        System.out.println("duplicate found ");
        compareSet.put(groupInteger, act + 1);
    } else {
        compareSet.put(groupInteger, 1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How do you find all subclasses of a given class in Java?
Possible Duplicate: Where can I find a Java decompiler? Hi, how to get the
Possible Duplicate: How can I find the missing value more concisely? Is there a
Possible Duplicate: Model.find(1) gives ActiveRecord error when id 1 does not exist If there
Possible Duplicate: How to find the kth largest element in an unsorted array of
Possible Duplicate: Console.Readline() max length? In my attempt to find a very simple text
Possible Duplicate: Length of Javascript Associative Array I have a JSON that looks like
Possible Duplicate: Object comparison in JavaScript Is there any method that takes in 2
Possible Duplicates: Does Java have a limit on the class name length? Max name
Possible Duplicate: How to find a list of wireless networks (SSID's) in Java, C#,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.