got it .
if ($linked_images)
{
$imageattcount = count($used_image_ids);
$lastimage = $imageattcount-1;
foreach ($linked_images as $attachment_id => $attachment)
{
//Check if the linked image still is in the post content, and if so set that as featured image.
if (in_array($attachment_id, $used_image_ids) && !has_post_thumbnail($post_id))
set_post_thumbnail($post_id, $lastimage);
}
}
its worked for me