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

  • SEARCH
  • Home
  • 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 586917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:09:24+00:00 2026-05-13T15:09:24+00:00

I have this code here, it is supposed to remove the common letters from

  • 0

I have this code here, it is supposed to remove the common letters from both the lists n1 and n2. But when i run this code it only runs once as in it removes only ‘a’ from both n1 and n2 and doesnt remove ‘k’.

Just to clarify this code should always work on only 2 words.

name1 = "abdjek"
name2 = "doarhsnk"

n1l = list(name1)
n2l = list(name2)

for i in range(len(n1l)):
   for j in range(len(n2l)):
         if n1l[i] == n2l[j]:
               n1l.pop(i)
               n2l.pop(j)
               n1l.append('0')
               n2l.append('1')

Ok wait, it seems to work for the above 2 names but when i have name1 = “naveen” and name2 = “darshana” it doesnt work!

  • 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-05-13T15:09:24+00:00Added an answer on May 13, 2026 at 3:09 pm

    A more pythonic approach would be to use sets and list comprehensions.

    name1 = "naveen"; name2 = "darshana"
    
    name1_set=set(name1)
    name2_set=set(name2)
    
    clean1=[x for x in  name1 if x not in name2_set]
    clean2=[x for x in name2 if x not in name1_set]
    
    clean1.extend(['0']*(len(name1)-len(clean1)))
    clean2.extend(['1']*(len(name2)-len(clean2)))
    
    print clean1,clean2
    

    set gives us O(1) lookups, thus making the whole process faster by making it O(N) instead of O(N^2).

    EDIT: In light of your later comment that the number of occurrences matter, this is the updated version that takes that into account.

    name1 = "naveen"; name2 = "darshana"
    
    count1={}
    count2={}
    
    
    for x in name1:
        count1[x]=count1.get(x,0)+1
    
    for x in name2:
        count2[x]=count2.get(x,0)+1
    
    def remove_dups(name,count,null):
        clean=[]
        for x in name:
            if count.get(x,0):
                count[x]-=1
            else:
                clean.append(x)
        clean.extend([null]*(len(name)-len(clean)))
        return clean
    
    clean1=remove_dups(name1,count2,'0')
    clean2=remove_dups(name2,count1,'1')
    
    print clean1,clean2
    

    It uses dicts to keep counts of occurrences. Whenever a character is removed, the corresponding count for the other name is decremented. Complexity is still O(N).

    It prints ['v', 'e', 'e', 'n', '0', '0'] and ['d', 'r', 's', 'h', 'a', 'a', '1', '1']. Is that what you wanted?

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

Sidebar

Ask A Question

Stats

  • Questions 308k
  • Answers 308k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Not pretty, but you could use lightweight object to hold… May 13, 2026 at 9:46 pm
  • Editorial Team
    Editorial Team added an answer If the xml format is totally outside your control you… May 13, 2026 at 9:46 pm
  • Editorial Team
    Editorial Team added an answer Don't forget the <cfoutput>! <cfoutput> <html> <head> <title>Test</title> </head> <body>… May 13, 2026 at 9:46 pm

Related Questions

I'm having issues with internet Explorer. I have a link which duplicates a file
I'm writing a simple custom owner-drawn ListBox control and banging my head against the
I am finding many solutions to this seemingly common problem, but alas, none seem
One of the assignments in my algorithms class is to design an exhaustive search
This seems like it would be a really easy thing to do, but its

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.