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 8245753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:17:20+00:00 2026-06-07T22:17:20+00:00

I have troubles understanding this code that detects duplicates in string. int checker =

  • 0

I have troubles understanding this code that detects duplicates in string.

int checker = 0;
    for(char ch : seed.toCharArray()){
        int val = ch - 'a';
        System.out.println(val);
        if ((checker & (1 << val)) > 0){ 
            // duplicate found
            break;
        }
        checker |= (1 << val);
    }

can someone explain me with an example how this works ?

  • 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-07T22:17:22+00:00Added an answer on June 7, 2026 at 10:17 pm

    Ok, say for example ch = 'c':

    int val = 'c' - 'a' = 2; //The char value for 'c' is two greater that that for 'a'
    

    then evaluate the if condition:

    1 << 2
    

    means “shift 1 two places to the left”, which gives us binary 100, or decimal 4.

    checker & 4
    

    means “bit-wise and” between checker, ie check if that particular bit is already set.

    if the bit was set, we are done, otherwise we perform

    c |= 4
    

    which means “c = c | 4”, which is a bit-wise or that will set the bit corresponding to ‘c’ to 1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hey there guys and girls i have this code that saves json as a
I am having trouble understanding this code: static long long _be_decode_int(const char **data, long
I have some troubles mapping a JSON Array to RestKit. This is what the
I understand that this problem is incredibly common and I have read through quite
I have trouble understanding what the following code means (and partly, why it even
I am having a bit of trouble understanding this bug. Here is the code:
This is the code from C++11 Notes Sample by Scott Meyers, int x; auto&&
I have some trouble understanding this one so here it is. I'm trying to
I'm having trouble understanding the following bit of code that I was hoping would
I'm having trouble understanding this code... I was expecting something similar to threading where

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.