Thread: Removing duplicate value in array

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    92

    Removing duplicate value in array

    I am trying to remove duplicates value in array

    sequence is [1, 2, 1, 2, 5]

    Program should give output [ 1, 2, 0, 0, 5]

    1 == 2
    1 == 1 found replace with zero 1 2 0 2 5
    1 == 2
    1 == 5

    2== 1
    2 == 2 found replace with zero 1 2 0 0 5
    1 == 2
    1 == 5
    ....... and son

    I wrote code but struggling with inner loop to update array value

    Code:
    #include<stdio.h>
    
    int main()
    {
    
    
    	int array[5] = { 1, 2, 1, 2, 5};
    
    
         for ( int i = 0; i < 5; i ++)
    	 {
    		 for ( int j = 0; j < 5; j++)
    		 {
    			 if ( array[i] == array[j+1])
    			 {
    				 
    				  
    			 } 
    		 }	
    	 }
    
    
         for ( int i = 0; i < 5; i ++)
    	 {
    		 printf("%d ", array[i]);
    	 }
       return 0;
    
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Start your loops like this.
    Code:
         for ( int i = 0; i < 5; i ++)
         {
             for ( int j = i+1; j < 5; j++)
    There's no point searching before where you've already got to.

    Then you're just comparing array[i] == array[j]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2022
    Posts
    92
    Quote Originally Posted by Salem View Post
    Start your loops like this.
    Code:
         for ( int i = 0; i < 5; i ++)
         {
             for ( int j = i+1; j < 5; j++)
    There's no point searching before where you've already got to.

    Then you're just comparing array[i] == array[j]
    Thank you

    Code:
     #include<stdio.h> 
    int main()
    {
        int array[5] = { 1, 2, 1, 2, 5};
     
    	for ( int i = 0; i < 5; i ++)
    	{
    		for ( int j = i+1; j < 5; j++)
    		{
                 if ( array[i] == array[j+1])
                 {
                      array[j+1] =0; 
                       
                 } 
             }  
         }
     
     
         for ( int i = 0; i < 5; i ++)
         {
             printf("%d ", array[i]);
         }
       return 0;
     
     
    }
    output

    Code:
     1 2 0 0 5

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Close, but you're stepping off the end of the array.
    Code:
    #include<stdio.h> 
    int main()
    {
        int array[5] = { 1, 2, 1, 2, 5};
     
        for ( int i = 0; i < 5; i ++)
        {
            for ( int j = i+1; j < 5; j++)
            {
                if ( array[i] == array[j])
                {
                    array[j] =0; 
                } 
            }
        }
    
        for ( int i = 0; i < 5; i ++)
        {
            printf("%d ", array[i]);
        }
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error message in removing duplicate strings
    By Tripswitch in forum C Programming
    Replies: 2
    Last Post: 08-10-2014, 01:28 AM
  2. removing duplicate elements from an array
    By Pulock2009 in forum C Programming
    Replies: 3
    Last Post: 11-06-2013, 01:25 PM
  3. removing a duplicate entry
    By csharp100 in forum C Programming
    Replies: 4
    Last Post: 04-20-2012, 10:02 AM
  4. Removing duplicate values from std::list
    By Phenom in forum C++ Programming
    Replies: 3
    Last Post: 11-03-2010, 12:59 PM
  5. Removing duplicate items from vector
    By 7heavens in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2010, 05:38 AM

Tags for this Thread